<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>NoSQL | VitePress</title>
    <meta name="description" content="A VitePress site">
    <link rel="preload stylesheet" href="/notebook/assets/style.3dbfd0c2.css" as="style">
    
    <script type="module" src="/notebook/assets/app.8aaa4cbe.js"></script>
    <link rel="preload" href="/notebook/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/notebook/assets/chunks/framework.1336c4e5.js">
    <link rel="modulepreload" href="/notebook/assets/chunks/theme.20cddc0c.js">
    <link rel="modulepreload" href="/notebook/assets/2、数据库_MongoDB_基础.md.fb7a0a29.lean.js">
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-255ec12d><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ae3e3f51></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ae3e3f51> Skip to content </a><!--]--><!----><header class="VPNav" data-v-255ec12d data-v-7e5bc4a5><div class="VPNavBar has-sidebar" data-v-7e5bc4a5 data-v-0937f67c><div class="container" data-v-0937f67c><div class="title" data-v-0937f67c><div class="VPNavBarTitle has-sidebar" data-v-0937f67c data-v-86d1bed8><a class="title" href="/notebook/" data-v-86d1bed8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/notebook/Vue.png" alt data-v-8426fc1a><!--]--><!--[-->任硕的文档<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-0937f67c><div class="curtain" data-v-0937f67c></div><div class="content-body" data-v-0937f67c><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-0937f67c><!--[--><!----><div id="docsearch"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-0937f67c data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>Java学前端</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/HTML+JS.html" data-v-2f2cfafc><!--[-->HTML+JS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/CSS.html" data-v-2f2cfafc><!--[-->CSS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue2+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue2+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue3+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue3+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/React.html" data-v-2f2cfafc><!--[-->React<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>软件测试</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->测试基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html" data-v-2f2cfafc><!--[-->压力测试<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>多线程</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-2f2cfafc><!--[-->基础篇<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%8C%E5%96%84.html" data-v-2f2cfafc><!--[-->进阶篇<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>开发工具</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/Chrome.html" data-v-2f2cfafc><!--[-->Chrome<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->IDEA基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E6%8F%92%E4%BB%B6.html" data-v-2f2cfafc><!--[-->IDEA插件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/VS%20Code.html" data-v-2f2cfafc><!--[-->VS Code<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>消息中间件</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ.html" data-v-2f2cfafc><!--[-->RabbitMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RocketMQ.html" data-v-2f2cfafc><!--[-->RocketMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Kafka.html" data-v-2f2cfafc><!--[-->Kafka<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Canal.html" data-v-2f2cfafc><!--[-->Canal<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-0937f67c data-v-f6a63727><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-f6a63727 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-0937f67c data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-0937f67c data-v-40855f84 data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-a7b5672a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-a7b5672a><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-40855f84><div class="item appearance" data-v-40855f84><p class="label" data-v-40855f84>Appearance</p><div class="appearance-action" data-v-40855f84><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-40855f84 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-40855f84><div class="item social-links" data-v-40855f84><div class="VPSocialLinks social-links-list" data-v-40855f84 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-0937f67c data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-255ec12d data-v-5cfd5582><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-5cfd5582><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-5cfd5582><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-5cfd5582>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-5cfd5582 data-v-18201f51><button data-v-18201f51>Return to top</button><!----></div></div><aside class="VPSidebar" data-v-255ec12d data-v-845b8fc6><div class="curtain" data-v-845b8fc6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-845b8fc6><span class="visually-hidden" id="sidebar-aria-label" data-v-845b8fc6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Java</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%9B%86%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java集合</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Linux</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Shell.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Shell脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实用脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>软件部署</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Nginx</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%AE%9E%E6%88%98%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实战篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E9%9D%A2%E8%AF%95%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>面试篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SSM</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Maven.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Maven</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Spring.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Spring</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringMVC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringMVC</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringBatch.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringBatch</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringBoot</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%BA%94%E7%94%A8%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>应用篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E8%BF%90%E7%BB%B4&amp;%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>运维&原理</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringCloud</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringCloud</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/Sentinel.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Sentinel</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringSecurity</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Mybatis & MybatisPlus</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/Mybatis.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Mybatis</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/MybatisPlus.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MybatisPlus</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/JPA.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JPA</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Git & ChatGPT</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Git.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Git</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Github.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Github</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/ChatGPT.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ChatGPT</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Jenkins.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Jenkins</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Netty</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible has-active" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>数据库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MySQL</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%AE%BE%E8%AE%A1.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL运维</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分库分表</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Redis</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis原理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis实战</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>本地缓存</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible has-active" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MongoDB</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>ElasticSearch</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/1%E3%80%81ES%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/3%E3%80%81ES%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/influxdb.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>InfluxDB</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Neo4j.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Neo4j</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>高并发 & 秒杀 & 分布式</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E5%88%86%E5%B8%83%E5%BC%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式理论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式锁</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E7%A7%92%E6%9D%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>秒杀</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%8F%AF%E7%94%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高可用</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%B9%B6%E5%8F%91.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高并发</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>云原生</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/Docker.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Docker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/K8S.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>K8S</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>可视化 & 监控</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%A7%E5%B1%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>可视化大屏</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/Zabbix.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Zabbix</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>学前端</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>HTML+CSS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/HTML%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>HTML基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/CSS%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>CSS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/%E7%BD%91%E9%A1%B5%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>网页进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>JS+TS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/TypeScript.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>TS基础</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>NodeJS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Vue</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E6%96%B0%E8%AF%AD%E6%B3%95.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3新语法</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue2/Vue2%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小程序</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/uniapp.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>uniapp</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>计算机基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>数据结构</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>操作系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>设计模式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>计算机网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/UML.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>UML</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E7%AE%97%E6%B3%95/LeetCode.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>LeetCode</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>项目实战</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>云尚办公</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E4%BA%91%E5%B0%9A%E5%8A%9E%E5%85%AC/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小兔鲜</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%871.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇1</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>地图</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>苍穹外卖</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E8%8B%8D%E7%A9%B9%E5%A4%96%E5%8D%96/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>黑马头条</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E6%94%AF%E4%BB%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>支付</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目推荐</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0" data-v-845b8fc6 data-v-9b797284><!----><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/team.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>团队成员</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-255ec12d data-v-669faec9><div class="VPDoc has-sidebar has-aside" data-v-669faec9 data-v-6b87e69f><!--[--><!--]--><div class="container" data-v-6b87e69f><div class="aside" data-v-6b87e69f><div class="aside-curtain" data-v-6b87e69f></div><div class="aside-container" data-v-6b87e69f><div class="aside-content" data-v-6b87e69f><div class="VPDocAside" data-v-6b87e69f data-v-3f215769><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-3f215769 data-v-ff0f39c8><div class="content" data-v-ff0f39c8><div class="outline-marker" data-v-ff0f39c8></div><div class="outline-title" data-v-ff0f39c8>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-ff0f39c8><span class="visually-hidden" id="doc-outline-aria-label" data-v-ff0f39c8> Table of Contents for current page </span><ul class="root" data-v-ff0f39c8 data-v-d0ee3533><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-6b87e69f><div class="content-container" data-v-6b87e69f><!--[--><!--]--><!----><main class="main" data-v-6b87e69f><div style="position:relative;" class="vp-doc _notebook_2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93_MongoDB_%E5%9F%BA%E7%A1%80" data-v-6b87e69f><div><h1 id="nosql" tabindex="-1">NoSQL <a class="header-anchor" href="#nosql" aria-label="Permalink to &quot;NoSQL&quot;">​</a></h1><blockquote><p>NoSQL(NoSQL = Not Only SQL )，意即&quot;不仅仅是SQL&quot;。在现代的计算系统上每天网络上都会产生庞大的数据量。这些数据有很大一部分是由关系数据库管理系统（RDBMS）来处理。 1970年 E.F.Codd&#39;s提出的关系模型的论文 &quot;A relational model of data for large shared data banks&quot;，这使得数据建模和应用程序编程更加简单。</p></blockquote><blockquote><p>通过应用实践证明，关系模型是非常适合于客户服务器编程，远远超出预期的利益，今天它是结构化数据存储在网络和商务应用的主导技术。NoSQL 是一项全新的数据库革命性运动，早期就有人提出，发展至2009年趋势越发高涨。NoSQL的拥护者们提倡运用非关系型的数据存储，相对于铺天盖地的关系型数据库运用，这一概念无疑是一种全新的思维的注入。</p></blockquote><h2 id="关系型数据库遵循acid规则" tabindex="-1">关系型数据库遵循ACID规则 <a class="header-anchor" href="#关系型数据库遵循acid规则" aria-label="Permalink to &quot;关系型数据库遵循ACID规则&quot;">​</a></h2><p>事务在英文中是transaction，和现实世界中的交易很类似，它有如下四个特性：</p><p><strong>1、A (Atomicity) 原子性</strong></p><p>原子性很容易理解，也就是说事务里的所有操作要么全部做完，要么都不做，事务成功的条件是事务里的所有操作都成功，只要有一个操作失败，整个事务就失败，需要回滚。比如银行转账，从A账户转100元至B账户，分为两个步骤：1）从A账户取100元；2）存入100元至B账户。这两步要么一起完成，要么一起不完成，如果只完成第一步，第二步失败，钱会莫名其妙少了100元。</p><p><strong>2、C (Consistency) 一致性</strong></p><p>一致性也比较容易理解，也就是说数据库要一直处于一致的状态，事务的运行不会改变数据库原本的一致性约束。例如现有完整性约束a+b=10，如果一个事务改变了a，那么必须得改变b，使得事务结束后依然满足a+b=10，否则事务失败。</p><p><strong>3、I (Isolation) 独立性</strong></p><p>所谓的独立性是指并发的事务之间不会互相影响，如果一个事务要访问的数据正在被另外一个事务修改，只要另外一个事务未提交，它所访问的数据就不受未提交事务的影响。比如现在有个交易是从A账户转100元至B账户，在这个交易还未完成的情况下，如果此时B查询自己的账户，是看不到新增加的100元的。</p><p><strong>4、D (Durability) 持久性</strong></p><p>持久性是指一旦事务提交后，它所做的修改将会永久的保存在数据库上，即使出现宕机也不会丢失。</p><h2 id="分布式系统" tabindex="-1">分布式系统 <a class="header-anchor" href="#分布式系统" aria-label="Permalink to &quot;分布式系统&quot;">​</a></h2><p>分布式系统（distributed system）由多台计算机和通信的软件组件通过计算机网络连接（本地网络或广域网）组成。</p><p>分布式系统是建立在网络之上的软件系统。正是因为软件的特性，所以分布式系统具有高度的内聚性和透明性。</p><p>因此，网络和分布式系统之间的区别更多的在于高层软件（特别是操作系统），而不是硬件。</p><p>分布式系统可以应用在不同的平台上如：Pc、工作站、局域网和广域网上等。</p><h2 id="分布式计算的优点" tabindex="-1">分布式计算的优点 <a class="header-anchor" href="#分布式计算的优点" aria-label="Permalink to &quot;分布式计算的优点&quot;">​</a></h2><blockquote><p><strong>可靠性（容错） ：分布式计算系统中的重要的优点是可靠性。一台服务器的系统崩溃不影响到其余服务器</strong></p><p>**可扩展性：**在分布式计算系统可以根据需要增加更多的机器。</p><p>**资源共享：**共享数据是必不可少的应用，如银行，预订系统。</p><p>**灵活性：**由于该系统是非常灵活的，它很容易安装，实施和调试新的服务。</p><p>**更快的速度：**分布式计算系统可以有多台计算机的计算能力，使得它比其他系统有更快的处理速度。</p><p>**开放系统：**由于它是开放的系统，本地或者远程都可以访问到该服务。</p><p>**更高的性能：**相较于集中式计算机网络集群可以提供更高的性能（及更好的性价比）。</p></blockquote><h2 id="分布式计算的缺点" tabindex="-1">分布式计算的缺点 <a class="header-anchor" href="#分布式计算的缺点" aria-label="Permalink to &quot;分布式计算的缺点&quot;">​</a></h2><blockquote><p>**故障排除：**故障排除和诊断问题。</p><p>**软件：**更少的软件支持是分布式计算系统的主要缺点。</p><p>**网络：**网络基础设施的问题，包括：传输问题，高负载，信息丢失等。</p><p>**安全性：**开放系统的特性让分布式计算系统存在着数据的安全性和共享的风险等问题。</p></blockquote><h2 id="什么是nosql" tabindex="-1">什么是NoSQL? <a class="header-anchor" href="#什么是nosql" aria-label="Permalink to &quot;什么是NoSQL?&quot;">​</a></h2><p>NoSQL，指的是非关系型的数据库。NoSQL有时也称作Not Only SQL的缩写，是对不同于传统的关系型数据库的数据库管理系统的统称。</p><p>NoSQL用于超大规模数据的存储。（例如谷歌或Facebook每天为他们的用户收集万亿比特的数据）。这些类型的数据存储不需要固定的模式，无需多余操作就可以横向扩展。</p><h2 id="为什么使用nosql" tabindex="-1">为什么使用NoSQL ? <a class="header-anchor" href="#为什么使用nosql" aria-label="Permalink to &quot;为什么使用NoSQL ?&quot;">​</a></h2><blockquote><p>今天我们可以通过第三方平台（如：Google,Facebook等）可以很容易的访问和抓取数据。用户的个人信息，社交网络，地理位置，用户生成的数据和用户操作日志已经成倍的增加。我们如果要对这些用户数据进行挖掘，那SQL数据库已经不适合这些应用了, NoSQL 数据库的发展却能很好的处理这些大的数据。</p></blockquote><h2 id="cap定理-cap-theorem" tabindex="-1">CAP定理（CAP theorem） <a class="header-anchor" href="#cap定理-cap-theorem" aria-label="Permalink to &quot;CAP定理（CAP theorem）&quot;">​</a></h2><p>在计算机科学中, CAP定理（CAP theorem）, 又被称作 布鲁尔定理（Brewer&#39;s theorem）, 它指出对于一个分布式计算系统来说，不可能同时满足以下三点:</p><ul><li><strong>一致性(Consistency)</strong> (所有节点在同一时间具有相同的数据)</li><li><strong>可用性(Availability)</strong> (保证每个请求不管成功或者失败都有响应)</li><li><strong>分区容错性(Partition tolerance)</strong> (系统中任意信息的丢失或失败不会影响系统的继续运作)</li></ul><p>CAP理论的核心是：一个分布式系统不可能同时很好的满足一致性，可用性和分区容错性这三个需求，最多只能同时较好的满足两个。</p><p>因此，根据 CAP 原理将 NoSQL 数据库分成了满足 CA 原则、满足 CP 原则和满足 AP 原则三 大类：</p><ul><li>CA - 单点集群，满足一致性，可用性的系统，通常在可扩展性上不太强大。</li><li>CP - 满足一致性，分区容忍性的系统，通常性能不是特别高。</li><li>AP - 满足可用性，分区容忍性的系统，通常可能对一致性要求低一些。</li></ul><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304231047195.png" alt="image-20230423104739112" style="zoom:67%;"><h2 id="nosql的优点-缺点" tabindex="-1">NoSQL的优点/缺点 <a class="header-anchor" href="#nosql的优点-缺点" aria-label="Permalink to &quot;NoSQL的优点/缺点&quot;">​</a></h2><p>优点:</p><ul><li>- 高可扩展性</li><li>- 分布式计算</li><li>- 低成本</li><li>- 架构的灵活性，半结构化数据</li><li>- 没有复杂的关系</li></ul><p>缺点:</p><ul><li>- 没有标准化</li><li>- 有限的查询功能（到目前为止）</li><li>- 最终一致是不直观的程序</li></ul><h2 id="base" tabindex="-1">BASE <a class="header-anchor" href="#base" aria-label="Permalink to &quot;BASE&quot;">​</a></h2><p>BASE：Basically Available, Soft-state, Eventually Consistent。 由 Eric Brewer 定义。</p><p>CAP理论的核心是：一个分布式系统不可能同时很好的满足一致性，可用性和分区容错性这三个需求，最多只能同时较好的满足两个。BASE是NoSQL数据库通常对可用性及一致性的弱要求原则:</p><ul><li>Basically Available --基本可用</li><li>Soft-state --软状态/柔性事务。 &quot;Soft state&quot; 可以理解为&quot;无连接&quot;的, 而 &quot;Hard state&quot; 是&quot;面向连接&quot;的</li><li>Eventually Consistency -- 最终一致性， 也是 ACID 的最终目的。</li></ul><h2 id="acid-vs-base" tabindex="-1">ACID vs BASE <a class="header-anchor" href="#acid-vs-base" aria-label="Permalink to &quot;ACID vs BASE&quot;">​</a></h2><table><thead><tr><th style="text-align:left;">ACID</th><th style="text-align:left;">BASE</th></tr></thead><tbody><tr><td style="text-align:left;">原子性(<strong>A</strong>tomicity)</td><td style="text-align:left;">基本可用(<strong>B</strong>asically <strong>A</strong>vailable)</td></tr><tr><td style="text-align:left;">一致性(<strong>C</strong>onsistency)</td><td style="text-align:left;">软状态/柔性事务(<strong>S</strong>oft state)</td></tr><tr><td style="text-align:left;">隔离性(<strong>I</strong>solation)</td><td style="text-align:left;">最终一致性 (<strong>E</strong>ventual consistency)</td></tr><tr><td style="text-align:left;">持久性 (<strong>D</strong>urable)</td><td style="text-align:left;"></td></tr></tbody></table><h2 id="nosql-数据库分类" tabindex="-1">NoSQL 数据库分类 <a class="header-anchor" href="#nosql-数据库分类" aria-label="Permalink to &quot;NoSQL 数据库分类&quot;">​</a></h2><table><thead><tr><th>类型</th><th>部分代表</th><th>特点</th></tr></thead><tbody><tr><td>列存储</td><td>HbaseCassandraHypertable</td><td>顾名思义，是按列存储数据的。最大的特点是方便存储结构化和半结构化数据，方便做数据压缩，对针对某一列或者某几列的查询有非常大的IO优势。</td></tr><tr><td>文档存储</td><td>MongoDBCouchDB</td><td>文档存储一般用类似json的格式存储，存储的内容是文档型的。这样也就有机会对某些字段建立索引，实现关系数据库的某些功能。</td></tr><tr><td>key-value存储</td><td>Tokyo Cabinet / TyrantBerkeley DBMemcacheDBRedis</td><td>可以通过key快速查询到其value。一般来说，存储不管value的格式，照单全收。（Redis包含了其他功能）</td></tr><tr><td>图存储</td><td>Neo4JFlockDB</td><td>图形关系的最佳存储。使用传统关系数据库来解决的话性能低下，而且设计使用不方便。</td></tr><tr><td>对象存储</td><td>db4oVersant</td><td>通过类似面向对象语言的语法操作数据库，通过对象的方式存取数据。</td></tr><tr><td>xml数据库</td><td>Berkeley DB XMLBaseX</td><td>高效的存储XML数据，并支持XML的内部查询语法，比如XQuery,Xpath。</td></tr></tbody></table><h1 id="安装、启动、配置" tabindex="-1">安装、启动、配置 <a class="header-anchor" href="#安装、启动、配置" aria-label="Permalink to &quot;安装、启动、配置&quot;">​</a></h1><h2 id="linux安装mongodb" tabindex="-1">Linux安装mongodb <a class="header-anchor" href="#linux安装mongodb" aria-label="Permalink to &quot;Linux安装mongodb&quot;">​</a></h2><p>下载官网[MongoDB Community Download | MongoDB</p><p><a href="https://www.mongodb.com/try/download/community" target="_blank" rel="noreferrer">https://www.mongodb.com/try/download/community</a></p><p><a href="https://www.mongodb.com/download-center/community/releases/archive" target="_blank" rel="noreferrer">https://www.mongodb.com/download-center/community/releases/archive</a></p><p>学习：Centos8安装MongoDB(<a href="https://www.cnblogs.com/phdeblog/p/14076066.html" target="_blank" rel="noreferrer">https://www.cnblogs.com/phdeblog/p/14076066.html</a>)</p><p>注意选择版本，centos8有对应centos8的版本</p><h3 id="安装配置" tabindex="-1">安装配置 <a class="header-anchor" href="#安装配置" aria-label="Permalink to &quot;安装配置&quot;">​</a></h3><p>上传压缩包到Linux中，解压到当前目录：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">tar </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">xvf mongodb</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">linux</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">x86_64</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">4.0.10</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">tgz</span></span></code></pre></div><p>移动解压后的文件夹到指定的目录中：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mv mongodb</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">linux</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">x86_64</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">4.0.10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span></span></code></pre></div><p>新建几个目录，分别用来存储数据和日志：</p><p>注意：他们是在根目录里</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">数据存储目录</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">日志存储目录</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span></span></code></pre></div><p>添加环境变量并刷新</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">、打开配置文件</span></span>
<span class="line"><span style="color:#A6ACCD;">vim  </span><span style="color:#89DDFF;">~/.</span><span style="color:#A6ACCD;">bash_profile</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">、在配置文件中加入</span></span>
<span class="line"><span style="color:#A6ACCD;">export PATH</span><span style="color:#89DDFF;">=/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bin:$PATH </span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">、保存退出，刷新</span></span>
<span class="line"><span style="color:#A6ACCD;">source </span><span style="color:#89DDFF;">~/.</span><span style="color:#A6ACCD;">bash_profile</span></span></code></pre></div><p>配置yml格式的配置文件</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vi </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#MongoDB发送所有日志输出的目标指定为文件</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;"># #The path of the log file to which mongod or mongos should send all diagnostic logging information</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/single/log/mongod.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#当mongos或mongod实例重新启动时，mongos或mongod会将新条目附加到现有日志文件的末尾。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">##The directory where the mongod instance stores its data.Default Value is &quot;/data/db&quot;.</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/single/data/db</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">#启用或禁用持久性日志以确保数据文件保持有效和可恢复。</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#启用在后台运行mongos或mongod进程的守护进程模式。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#服务实例绑定的IP，默认是localhost</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#bindIp</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#绑定的端口，默认是27017</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27017</span></span></code></pre></div><p>注意：上面配置的ip地址是要在linux有网才能运行，不然会报错</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 正常启动</span></span>
<span class="line"><span style="color:#FFCB6B;">/usr/local/mongodb/bin/mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/mongodb/single/mongod.conf</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置完环境变量启动</span></span>
<span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/mongodb/single/mongod.conf</span></span></code></pre></div><p>出现successful就是成功</p><p>此时再输入mongo就能进入数据库</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo</span></span></code></pre></div><p>连接测试</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20211109091015159.png" alt="image-20211109091015159" style="zoom:80%;"><p>关闭数据库</p><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">shutdown </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">dbpath</span><span style="color:#89DDFF;">=/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span></span></code></pre></div><h3 id="navicate显示系统库" tabindex="-1">Navicate显示系统库 <a class="header-anchor" href="#navicate显示系统库" aria-label="Permalink to &quot;Navicate显示系统库&quot;">​</a></h3><h3 id="配置环境变量" tabindex="-1">配置环境变量 <a class="header-anchor" href="#配置环境变量" aria-label="Permalink to &quot;配置环境变量&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 1、打开配置文件</span></span>
<span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">~/.bash_profile</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 2、在配置文件中加入</span></span>
<span class="line"><span style="color:#C792EA;">export</span><span style="color:#A6ACCD;"> PATH</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">/usr/local/mongo/bin:</span><span style="color:#A6ACCD;">$PATH </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 3、保存退出，刷新</span></span>
<span class="line"><span style="color:#82AAFF;">source</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">~/.bash_profile</span></span></code></pre></div><p>配置完成后启动</p><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod</span></span></code></pre></div><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo</span></span></code></pre></div><h3 id="查看系统进程-linux" tabindex="-1">查看系统进程(Linux) <a class="header-anchor" href="#查看系统进程-linux" aria-label="Permalink to &quot;查看系统进程(Linux)&quot;">​</a></h3><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ps -ef |grep mongod</span></span></code></pre></div><p>如果连接不上，关闭防火墙</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#查看防火墙状态</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">status</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">firewalld</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#临时关闭防火墙</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stop</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">firewalld</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#开机禁止启动防火墙</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">disable</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">firewalld</span></span></code></pre></div><h3 id="停止关闭数据库" tabindex="-1">停止关闭数据库 <a class="header-anchor" href="#停止关闭数据库" aria-label="Permalink to &quot;停止关闭数据库&quot;">​</a></h3><p>停止服务的方式有两种：</p><p>快速关闭和标准关闭，下面依次说明：</p><p>快速关闭方法（快速，简单，数据可能会出错）</p><p>目标：通过系统的kill命令直接杀死进程： 杀完要检查一下，避免有的没有杀掉。</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#通过进程编号关闭节点</span></span>
<span class="line"><span style="color:#A6ACCD;">kill -2 54410</span></span></code></pre></div><p>【补充】 如果一旦是因为数据损坏，则需要进行如下操作（了解）：</p><p>1）删除lock文件：</p><div class="language-typescript"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#676E95;font-style:italic;">/*.lock</span></span></code></pre></div><p>2）修复数据：</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">/usr/local/mongo/bin/mongod --repair --dbpath=/mongodb/single/data/db</span></span></code></pre></div><p>标准的关闭方法（数据不容易出错，但麻烦）</p><p>目标：通过mongo客户端中的shutdownServer命令来关闭服务 主要的操作步骤参考如下：</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 客户端登录服务，注意，这里通过localhost登录，如果需要远程登录，必须先登录认证才行。</span></span>
<span class="line"><span style="color:#FFCB6B;">mongo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27017</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#切换到admin库</span></span>
<span class="line"><span style="color:#FFCB6B;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">admin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 关闭服务</span></span>
<span class="line"><span style="color:#82AAFF;">db.shutdownServer</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h2 id="win版本安装" tabindex="-1">win版本安装 <a class="header-anchor" href="#win版本安装" aria-label="Permalink to &quot;win版本安装&quot;">​</a></h2><p>官网：<a href="https://www.mongodb.com/try/download/community" target="_blank" rel="noreferrer">https://www.mongodb.com/try/download/community</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221551041.png" alt="image-20230422155146963" style="zoom:80%;"><blockquote><p>选择complete完整安装，设置data和log路径</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221555926.png" alt="image-20230422155525853" style="zoom:80%;"><blockquote><p>搜索进入服务，查看mongoDB是否启动</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221559803.png" alt="image-20230422155916737" style="zoom:80%;"><h1 id="mongodb-基础" tabindex="-1">MongoDB 基础 <a class="header-anchor" href="#mongodb-基础" aria-label="Permalink to &quot;MongoDB 基础&quot;">​</a></h1><h2 id="基本描述" tabindex="-1">基本描述 <a class="header-anchor" href="#基本描述" aria-label="Permalink to &quot;基本描述&quot;">​</a></h2><blockquote><p>MongoDB 是一个基于 <strong>分布式文件存储</strong> 的开源 NoSQL 数据库系统，由 <strong>C++</strong> 编写的。MongoDB 提供了 <strong>面向文档</strong> 的存储方式，操作起来比较简单和容易，支持“<strong>无模式</strong>”的数据建模，可以存储比较复杂的数据类型，是一款非常流行的 <strong>文档类型数据库</strong> 。</p></blockquote><blockquote><p>在高负载的情况下，MongoDB 天然支持水平扩展和高可用，可以很方便地添加更多的节点/实例，以保证服务性能和可用性。在许多场景下，MongoDB 可以用于代替传统的关系型数据库或键/值存储方式，皆在为 Web 应用提供可扩展的高可用高性能数据存储解决方案。</p></blockquote><h2 id="概念解析" tabindex="-1">概念解析 <a class="header-anchor" href="#概念解析" aria-label="Permalink to &quot;概念解析&quot;">​</a></h2><table><thead><tr><th style="text-align:left;">SQL术语/概念</th><th style="text-align:left;">MongoDB术语/概念</th><th style="text-align:left;">解释/说明</th></tr></thead><tbody><tr><td style="text-align:left;">database</td><td style="text-align:left;">database</td><td style="text-align:left;">数据库</td></tr><tr><td style="text-align:left;">table</td><td style="text-align:left;">collection</td><td style="text-align:left;">数据库表/集合</td></tr><tr><td style="text-align:left;">row</td><td style="text-align:left;">document</td><td style="text-align:left;">数据记录行/文档</td></tr><tr><td style="text-align:left;">column</td><td style="text-align:left;">field</td><td style="text-align:left;">数据字段/域</td></tr><tr><td style="text-align:left;">index</td><td style="text-align:left;">index</td><td style="text-align:left;">索引</td></tr><tr><td style="text-align:left;">table joins</td><td style="text-align:left;"></td><td style="text-align:left;">表连接,MongoDB不支持</td></tr><tr><td style="text-align:left;">primary key</td><td style="text-align:left;">primary key</td><td style="text-align:left;">主键,MongoDB自动将_id字段设置为主键</td></tr></tbody></table><p>通过下图实例，我们也可以更直观的了解Mongo中的一些概念：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304231055934.png" alt="image-20230423105505870" style="zoom:80%;"><h2 id="数据库、文档、集合" tabindex="-1">数据库、文档、集合 <a class="header-anchor" href="#数据库、文档、集合" aria-label="Permalink to &quot;数据库、文档、集合&quot;">​</a></h2><h3 id="数据库" tabindex="-1">数据库 <a class="header-anchor" href="#数据库" aria-label="Permalink to &quot;数据库&quot;">​</a></h3><blockquote><p>数据库用于存储所有集合，而集合又用于存储所有文档。一个 MongoDB 中可以创建多个数据库，每一个数据库都有自己的集合和权限。mongodb中可以建立多个数据库。MongoDB的默认数据库为&quot;db&quot;，该数据库存储在data目录中。单个实例可以容纳多个独立数据库，每一个都有自己的集合和权限，不同的数据库也放置在不同的文件中MongoDB 预留了几个特殊的数据库。</p></blockquote><blockquote><p><strong>admin</strong> : admin 数据库主要是保存 root 用户和角色。例如，system.users 表存储用户，system.roles 表存储角色。一般不建议用户直接操作这个数据库。将一个用户添加到这个数据库，且使它拥有 admin 库上的名为 dbAdminAnyDatabase 的角色权限，这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行，比如关闭服务器。</p></blockquote><blockquote><p><strong>local</strong> : local 数据库是不会被复制到其他分片的，因此可以用来存储本地单台服务器的任意 collection。一般不建议用户直接使用 local 库存储任何数据，也不建议进行 CRUD 操作，因为数据无法被正常备份与恢复。</p></blockquote><blockquote><p><strong>config</strong> : 当 MongoDB 使用分片设置时，config 数据库可用来保存分片的相关信息。</p></blockquote><blockquote><p><strong>test</strong> : 默认创建的测试库，连接 mongod 服务时，如果不指定连接的具体数据库，默认就会连接到 test 数据库</p></blockquote><h3 id="文档" tabindex="-1">文档 <a class="header-anchor" href="#文档" aria-label="Permalink to &quot;文档&quot;">​</a></h3><blockquote><p>文档是一组键值(key-value)对(即 BSON)。MongoDB 的文档不需要设置相同的字段，并且相同的字段不需要相同的数据类型，这与关系型数据库有很大的区别，也是 MongoDB 非常突出的特点。一个简单的文档例子如下：</p></blockquote><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C792EA;">site</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">www.runoob.com</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C792EA;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">菜鸟教程</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>下表列出了 RDBMS 与 MongoDB 对应的术语：</p><table><thead><tr><th style="text-align:left;">RDBMS</th><th style="text-align:left;">MongoDB</th></tr></thead><tbody><tr><td style="text-align:left;">数据库</td><td style="text-align:left;">数据库</td></tr><tr><td style="text-align:left;">表格</td><td style="text-align:left;">集合</td></tr><tr><td style="text-align:left;">行</td><td style="text-align:left;">文档</td></tr><tr><td style="text-align:left;">列</td><td style="text-align:left;">字段</td></tr><tr><td style="text-align:left;">表联合</td><td style="text-align:left;">嵌入文档</td></tr><tr><td style="text-align:left;">主键</td><td style="text-align:left;">主键 (MongoDB 提供了 key 为 _id )</td></tr><tr><td style="text-align:left;">数据库服务和客户端</td><td style="text-align:left;"></td></tr><tr><td style="text-align:left;">Mysqld/Oracle</td><td style="text-align:left;">mongodb</td></tr><tr><td style="text-align:left;">mysql/sqlplus</td><td style="text-align:left;">mongo</td></tr></tbody></table><p>需要注意的是：</p><blockquote><ol><li>文档中的键/值对是有序的。</li><li>文档中的值不仅可以是在双引号里面的字符串，还可以是其他几种数据类型（甚至可以是整个嵌入的文档)。</li><li>MongoDB区分类型和大小写。</li><li>MongoDB的文档不能有重复的键。</li><li>文档的键是字符串。除了少数例外情况，键可以使用任意UTF-8字符。</li></ol></blockquote><h3 id="集合" tabindex="-1">集合 <a class="header-anchor" href="#集合" aria-label="Permalink to &quot;集合&quot;">​</a></h3><blockquote><p>MongoDB 集合存在于数据库中，<strong>没有固定的结构</strong>，也就是 <strong>无模式</strong> 的，这意味着可以往集合插入不同格式和类型的数据。不过，通常情况相爱插入集合中的数据都会有一定的关联性。</p></blockquote><p>比如，我们可以将以下不同数据结构的文档插入到集合中：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">site</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">www.baidu.com</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">site</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">www.google.com</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Google</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">site</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">www.runoob.com</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">菜鸟教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">num</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">}</span></span></code></pre></div><h2 id="应用场景" tabindex="-1">应用场景 <a class="header-anchor" href="#应用场景" aria-label="Permalink to &quot;应用场景&quot;">​</a></h2><blockquote><p>社交场景，使用 MongoDB <strong>存储存储用户信息</strong>，以及<strong>用户发表的朋友圈信息</strong>，通过<strong>地理位置索引实现附近的人、地点</strong>等功能。</p></blockquote><blockquote><p>游戏场景，使用 MongoDB <strong>存储游戏用户信息</strong>，<strong>用户的装备</strong>、<strong>积分</strong>等直接以内嵌文档的形式存储，方便查询、高效率存储和访问。</p></blockquote><blockquote><p>物流场景，使用 MongoDB 存储订单信息，<strong>订单状态在运送过程中会不断更新</strong>，以 MongoDB 内嵌数组的形式来存储，一次查询就能将订单所有的变更读取出来。</p></blockquote><blockquote><p>物联网场景，使用 MongoDB <strong>存储所有接入的智能设备信息</strong>，以及<strong>设备汇报的日志信息</strong>，并对这些信息进行多维度的分析。</p></blockquote><blockquote><p>视频直播，使用 MongoDB <strong>存储用户信息</strong>、<strong>点赞互动信息</strong>等。</p></blockquote><p>这些应用场景中，数据操作方面的共同特点是：</p><blockquote><p>（1）<strong>数据量大</strong></p><p>（2）<strong>写入操作频繁</strong>（读写都很频繁）</p><p>（3）<strong>价值较低的数据，对事务性要求不高</strong></p></blockquote><p>对于这样的数据，我们更适合使用MongoDB来实现数据的存储。</p><h2 id="mysql表转换" tabindex="-1">MySQL表转换 <a class="header-anchor" href="#mysql表转换" aria-label="Permalink to &quot;MySQL表转换&quot;">​</a></h2><p>在mysql数据库中，表鼠标右键选择导出向导</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20211123105335479.png" alt="image-20211123105335479" style="zoom:67%;"><p>后面就是选择自己要导出的表一直下一步即可</p><p>在mongodb数据库，右键导入向导，同样选择Excel</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20211123105525824.png" alt="image-20211123105525824" style="zoom:80%;"><p>一直下一步即可</p><h2 id="体系架构" tabindex="-1">体系架构 <a class="header-anchor" href="#体系架构" aria-label="Permalink to &quot;体系架构&quot;">​</a></h2><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220410153019208.png" alt="image-20220410153019208"></p><h1 id="基本常用命令" tabindex="-1">基本常用命令 <a class="header-anchor" href="#基本常用命令" aria-label="Permalink to &quot;基本常用命令&quot;">​</a></h1><h2 id="shell连接" tabindex="-1">shell连接 <a class="header-anchor" href="#shell连接" aria-label="Permalink to &quot;shell连接&quot;">​</a></h2><p>注意要提前配置好环境变量</p><p>Linux上启动</p><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongo</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bin</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">single</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod.conf</span></span></code></pre></div><p>Linux和win上都可以</p><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">host</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">127.0.0.1 </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27017</span></span></code></pre></div><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo</span></span></code></pre></div><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27017</span></span></code></pre></div><p>加上端口号的目的是以后连接linux的mongodb就可以把127.0.0.1换成linux的端口号</p><h2 id="特殊数据库" tabindex="-1">特殊数据库 <a class="header-anchor" href="#特殊数据库" aria-label="Permalink to &quot;特殊数据库&quot;">​</a></h2><blockquote><p><strong>admin</strong>： 从权限的角度来看，这是&quot;root&quot;数据库。要是将一个用户添加到这个数据库，这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行，比如列出所有的数据库或者关闭服务器。</p></blockquote><blockquote><p><strong>local:</strong> 这个数据永远不会被复制，可以用来存储限于本地单台服务器的任意集合</p></blockquote><blockquote><p><strong>config</strong>: 当Mongo用于分片设置时，confifig数据库在内部使用，用于保存分片的相关信息。</p></blockquote><h2 id="数据库操作" tabindex="-1">数据库操作 <a class="header-anchor" href="#数据库操作" aria-label="Permalink to &quot;数据库操作&quot;">​</a></h2><h3 id="选择和创建数据库" tabindex="-1">选择和创建数据库 <a class="header-anchor" href="#选择和创建数据库" aria-label="Permalink to &quot;选择和创建数据库&quot;">​</a></h3><blockquote><p>注意 在 MongoDB 中，集合只有在内容插入后才会创建! 就是说，创建集合(数据表)后要再插入一个文档(记录)，集合才会真正创建。<strong>如果数据库不存在则自动创建</strong></p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 选择和创建数据库的语法格式</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> 数据库名称 </span></span>
<span class="line"><span style="color:#A6ACCD;"># 创建 articledb 数据库</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> articledb</span></span></code></pre></div><h3 id="查看数据库" tabindex="-1">查看数据库 <a class="header-anchor" href="#查看数据库" aria-label="Permalink to &quot;查看数据库&quot;">​</a></h3><blockquote><p>MongoDB 中默认的数据库为 test，如果你没有选择数据库，集合将存放在 test 数据库中</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 查看正在使用的数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">db  </span></span>
<span class="line"><span style="color:#A6ACCD;"># 查看所有数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">show dbs</span></span></code></pre></div><h3 id="删除数据库" tabindex="-1">删除数据库 <a class="header-anchor" href="#删除数据库" aria-label="Permalink to &quot;删除数据库&quot;">​</a></h3><p>MongoDB 删除数据库的语法格式如下：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">dropDatabase</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>提示：主要用来删除已经持久化的数据库</p><h3 id="查看当前库中的表" tabindex="-1">查看当前库中的表 <a class="header-anchor" href="#查看当前库中的表" aria-label="Permalink to &quot;查看当前库中的表&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show collections 或  show tables</span></span></code></pre></div><h2 id="集合操作" tabindex="-1">集合操作 <a class="header-anchor" href="#集合操作" aria-label="Permalink to &quot;集合操作&quot;">​</a></h2><p>集合，类似关系型数据库中的表。可以显示的创建，也可以隐式的创建。</p><h3 id="集合的显式创建-了解" tabindex="-1">集合的显式创建(了解) <a class="header-anchor" href="#集合的显式创建-了解" aria-label="Permalink to &quot;集合的显式创建(了解)&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.createCollection(</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>参数说明：name: 要创建的集合名称</p><p>例如：创建一个名为 mycollection 的普通集合。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.createCollection(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mycollection</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><h3 id="集合的隐式创建⭐" tabindex="-1">集合的隐式创建⭐ <a class="header-anchor" href="#集合的隐式创建⭐" aria-label="Permalink to &quot;集合的隐式创建⭐&quot;">​</a></h3><blockquote><p>当向一个集合中插入一个文档的时候，<strong>如果集合不存在，则会自动创建集合</strong>。通常我们使用隐式创建文档即可。</p></blockquote><h3 id="集合的删除" tabindex="-1">集合的删除 <a class="header-anchor" href="#集合的删除" aria-label="Permalink to &quot;集合的删除&quot;">​</a></h3><p>集合删除语法格式如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.</span><span style="color:#F78C6C;">drop</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">  或 db.集合.</span><span style="color:#F78C6C;">drop</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h2 id="固定集合" tabindex="-1">固定集合 <a class="header-anchor" href="#固定集合" aria-label="Permalink to &quot;固定集合&quot;">​</a></h2><blockquote><p>MongoDB 固定集合（Capped Collections）是性能出色且有着固定大小的集合，对于大小固定，我们可以想象其就像一个环形队列，当集合空间用完后，再插入的元素就会覆盖最初始的头部的元素！应用场景：储存日志信息，缓存一些少量的文档</p></blockquote><blockquote><p>固定集合使用固定大小的预分配空间（Pre-allocated Space），它们在创建时指定集合最多可以存储的文档数量或者是数据占用空间的大小。当集合的大小达到限制时，如果继续插入文档，最早插入的文档将会被覆盖。因此，固定集合适用于需要轮询（轮询的间隔时间可以是任意时间段）数据的应用场景，例如日志文件、定时任务等等。</p></blockquote><h3 id="创建固定集合" tabindex="-1">创建固定集合 <a class="header-anchor" href="#创建固定集合" aria-label="Permalink to &quot;创建固定集合&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">db.createCollection(name,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">capped:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">true</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">size:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">size,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">max:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">max</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>其中，<code>name</code>是固定集合的名称，<code>capped</code>设置为<code>true</code>表示创建的是固定集合，<code>size</code>表示固定集合的大小，以字节为单位，<code>max</code>表示固定集合中可以存储的最大文档数量。</p><p><strong>例如，如果要创建一个最多可以存储1000个文档的固定集合</strong>：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.createCollection(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myCollection</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">, { capped: true, size: </span><span style="color:#F78C6C;">1048576</span><span style="color:#A6ACCD;">, max: </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> })</span></span></code></pre></div><p>上述示例中，<code>size</code>参数设置为1 MB，<code>max</code>参数设置为1000个文档。</p><blockquote><p>需要注意的是，一旦创建了固定集合，就无法再将其转换为非固定集合。如果您需要更改固定集合的大小，需要先删除集合，然后重新创建一个新的固定集合。固定集合的优点是它们对磁盘空间的利用率更高，可以在某些场景下提高性能，但是由于其大小是固定的，因此在一些应用场景下可能会有限制。</p></blockquote><p><strong>判断集合是否为固定集合</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.cappedLogCollection.isCapped</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p><strong>如果需要将已存在的集合转换为固定集合可以使用以下命令</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.runCommand({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">convertToCapped</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">posts</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,size:</span><span style="color:#F78C6C;">10000</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>以上代码将我们已存在的 posts 集合转换为固定集合。</p><h3 id="固定集合查询" tabindex="-1">固定集合查询 <a class="header-anchor" href="#固定集合查询" aria-label="Permalink to &quot;固定集合查询&quot;">​</a></h3><p>固定集合文档按照插入顺序储存的,默认情况下查询就是按照插入顺序返回的,也可以使用$natural调整返回顺序。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.cappedLogCollection.find</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">.sort({$natural:</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><blockquote><p>可以插入及更新,但更新不能超出collection的大小,否则更新失败,不允许删除,但是可以调用drop()删除集合中的所有行,但是drop后需要显式地重建集合。在32位机子上一个cappped collection的最大值约为482.5M,64位上只受系统文件大小的限制。</p></blockquote><h2 id="gridfs" tabindex="-1">GridFS <a class="header-anchor" href="#gridfs" aria-label="Permalink to &quot;GridFS&quot;">​</a></h2><blockquote><p>GridFS 用于存储和恢复那些超过16M（BSON文件限制）的文件(如：图片、音频、视频等)。GridFS 也是文件存储的一种方式，但是它是存储在MonoDB的集合中。GridFS 可以更好的存储大于16M的文件。</p></blockquote><blockquote><p>GridFS 会将大文件对象分割成多个小的chunk(文件片段),一般为256k/个,每个chunk将作为MongoDB的一个文档(document)被存储在chunks集合中。GridFS 用两个集合存储一个文件：fs.files与fs.chunks。每个文件的实际内容被存在chunks(二进制数据)中,和文件有关的meta数据(filename,content_type,还有用户自定义属性)会被存在files集合</p></blockquote><blockquote><p>存在问题：比如它读取文件的性能并不是很好，因为我们需要查询两次集合（先是fs.files，再是fs.chunks）才能将文件拼凑出来。如果我们的文件需要经常修改，那么GridFS也不合适，因为GridFS没法修改单独的某个分块，要修改文件的话，需要先将该文件删除，然后重新上传。</p></blockquote><blockquote><p>在Navicat中直接可以进行操作</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221729727.png" alt="image-20230422172957654" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221729501.png" alt="image-20230422172921420" style="zoom:80%;"><h1 id="数据类型" tabindex="-1">数据类型 <a class="header-anchor" href="#数据类型" aria-label="Permalink to &quot;数据类型&quot;">​</a></h1><table><thead><tr><th style="text-align:left;">数据类型</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;">String</td><td style="text-align:left;">字符串。存储数据常用的数据类型。在 MongoDB 中，UTF-8 编码的字符串才是合法的。</td></tr><tr><td style="text-align:left;">Integer</td><td style="text-align:left;">整型数值。用于存储数值。根据你所采用的服务器，可分为 32 位或 64 位。</td></tr><tr><td style="text-align:left;">Boolean</td><td style="text-align:left;">布尔值。用于存储布尔值（真/假）。</td></tr><tr><td style="text-align:left;">Double</td><td style="text-align:left;">双精度浮点值。用于存储浮点值。</td></tr><tr><td style="text-align:left;">Min/Max keys</td><td style="text-align:left;">将一个值与 BSON（二进制的 JSON）元素的最低值和最高值相对比。</td></tr><tr><td style="text-align:left;">Array</td><td style="text-align:left;">用于将数组或列表或多个值存储为一个键。</td></tr><tr><td style="text-align:left;">Timestamp</td><td style="text-align:left;">时间戳。记录文档修改或添加的具体时间。</td></tr><tr><td style="text-align:left;">Object</td><td style="text-align:left;">用于内嵌文档。</td></tr><tr><td style="text-align:left;">Null</td><td style="text-align:left;">用于创建空值。</td></tr><tr><td style="text-align:left;">Symbol</td><td style="text-align:left;">符号。该数据类型基本上等同于字符串类型，但不同的是，它一般用于采用特殊符号类型的语言。</td></tr><tr><td style="text-align:left;">Date</td><td style="text-align:left;">日期时间。用 UNIX 时间格式来存储当前日期或时间。你可以指定自己的日期时间：创建 Date 对象，传入年月日信息。</td></tr><tr><td style="text-align:left;">Object ID</td><td style="text-align:left;">对象 ID。用于创建文档的 ID。</td></tr><tr><td style="text-align:left;">Binary Data</td><td style="text-align:left;">二进制数据。用于存储二进制数据。</td></tr><tr><td style="text-align:left;">Code</td><td style="text-align:left;">代码类型。用于在文档中存储 JavaScript 代码。</td></tr><tr><td style="text-align:left;">Regular expression</td><td style="text-align:left;">正则表达式类型。用于存储正则表达式。</td></tr></tbody></table><h2 id="主键id" tabindex="-1">主键ID <a class="header-anchor" href="#主键id" aria-label="Permalink to &quot;主键ID&quot;">​</a></h2><blockquote><p>在 MongoDB 中，<code>ObjectId</code> 是一种特殊的数据类型，用于唯一标识一个文档（document）。每个 <code>ObjectId</code> 都是一个 12 字节的十六进制字符串。MongoDB采用ObjectId，而不是其他比较常规的做法（比如自动增加的主键）的主要原因，<strong>因为在多个 服务器上同步自动增加主键值既费力还费时。</strong></p></blockquote><h3 id="组成部分" tabindex="-1">组成部分 <a class="header-anchor" href="#组成部分" aria-label="Permalink to &quot;组成部分&quot;">​</a></h3><blockquote><p>4 字节的时间戳（timestamp），表示文档创建时间，精确到秒级别。</p><p>3 字节的机器标识码（machine identifier），表示生成 <code>ObjectId</code> 的机器的标识。</p><p>2 字节的进程 ID（process id），表示在同一秒内生成 <code>ObjectId</code> 的不同进程的标识。</p><p>3 字节的随机数（random number），保证在同一秒内生成 <code>ObjectId</code> 的不同进程生成不同的值，避免冲突</p></blockquote><blockquote><p>由于 <code>ObjectId</code> 包含了文档的创建时间，因此可以按时间范围查询文档，还可以按 <code>_id</code> 字段进行排序。此外，由于 <code>ObjectId</code> 在分布式系统中具有唯一性，因此可以用作分布式系统中文档的唯一标识符。</p></blockquote><h3 id="生成id" tabindex="-1">生成ID <a class="header-anchor" href="#生成id" aria-label="Permalink to &quot;生成ID&quot;">​</a></h3><blockquote><p>插入数据时，无需写id，写其他字段就行，id会自动生成的</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.</span><span style="color:#F78C6C;">insert</span><span style="color:#A6ACCD;">({</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  age: </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  _id: ObjectId</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">});</span></span></code></pre></div><h3 id="获取时间戳⭐" tabindex="-1">获取时间戳⭐ <a class="header-anchor" href="#获取时间戳⭐" aria-label="Permalink to &quot;获取时间戳⭐&quot;">​</a></h3><blockquote><p>由于 ObjectId 中存储了 4 个字节的时间戳，所以你不需要为你的文档保存时间戳字段，你可以通过 getTimestamp 函数来获取文档的创建时间</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 结果：ISODate(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">2023-04-22T09:00:21.000Z</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">ObjectId(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">6443a225ce79a828af66a64e</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">).getTimestamp</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h3 id="转成字符串" tabindex="-1">转成字符串 <a class="header-anchor" href="#转成字符串" aria-label="Permalink to &quot;转成字符串&quot;">​</a></h3><blockquote><p>在某些情况下，您可能需要将ObjectId转换为字符串格式。你可以使用下面的代码</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">new ObjectId</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">.str</span></span></code></pre></div><h2 id="基础数据类型" tabindex="-1">基础数据类型 <a class="header-anchor" href="#基础数据类型" aria-label="Permalink to &quot;基础数据类型&quot;">​</a></h2><p>String：字符串类型，最常用的数据类型之一，用于存储文本信息。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><p>Number：数值类型，可以分为整数和浮点数。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">age</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">28</span></span>
<span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">score</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">88.5</span></span></code></pre></div><p>Boolean：布尔类型，只有两个值：<code>true</code>和<code>false</code>。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">isStudent</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">true</span></span></code></pre></div><p>Object：文档类型，用于嵌套存储子文档。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">address</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C792EA;">city</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Beijing</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C792EA;">country</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">China</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>Array：数组类型，用于存储一组有序的值。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">scores</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">90</span><span style="color:#89DDFF;">]</span></span></code></pre></div><p>Null：空类型，表示不存在的值。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">middleName</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">null</span></span></code></pre></div><h2 id="高级数据类型" tabindex="-1">高级数据类型 <a class="header-anchor" href="#高级数据类型" aria-label="Permalink to &quot;高级数据类型&quot;">​</a></h2><p>Date：日期类型，用于存储日期和时间。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">birthday</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: ISODate(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">1995-06-20T00:00:00Z</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">new Date() </span></span>
<span class="line"><span style="color:#A6ACCD;">ISODate()</span></span></code></pre></div><p>ObjectId：文档ID类型，是MongoDB中每个文档的唯一标识符。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: ObjectId(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">615f6807e8a1f814143e7fa6</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>Binary：二进制数据类型，用于存储二进制数据。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">avatar</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: BinData(</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">iVBORw0KGg...</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>Regular Expression：正则表达式类型，用于存储正则表达式。例如：</p><div class="language-json"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">pattern</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: /hello world/i</span></span></code></pre></div><h2 id="实战演练" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练" aria-label="Permalink to &quot;实战演练&quot;">​</a></h2><blockquote><p>这个文档包括了字符串、整数、浮点数、布尔值、null、ObjectID、日期、数组、对象、二进制数据、正则表达式、DBPointer、JavaScript代码、Symbol、Int32、Timestamp和Decimal128等多种数据类型。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.myCollection.insertOne({</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">stringField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Hello World</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">intField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">42</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">doubleField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">booleanField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: true,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">nullField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">objectIdField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: ObjectId</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">dateField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">arrayField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: [1, 2, 3],</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">objectField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: {</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">foo</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">bar</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">binaryDataField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: new BinData(</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">AQ==</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">regexField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">dbPointerField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: new DBPointer(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myOtherCollection</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">, ObjectId</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">javascriptField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">function</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> { </span><span style="color:#F78C6C;">return</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">; },</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">symbolField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: Symbol(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mySymbol</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">int32Field</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: NumberInt(</span><span style="color:#F78C6C;">42</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">timestampField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#C792EA;">Timestamp</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">decimalField</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: NumberDecimal(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">2.5</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h1 id="增删改查" tabindex="-1">增删改查 <a class="header-anchor" href="#增删改查" aria-label="Permalink to &quot;增删改查&quot;">​</a></h1><h2 id="插入文档" tabindex="-1">插入文档 <a class="header-anchor" href="#插入文档" aria-label="Permalink to &quot;插入文档&quot;">​</a></h2><h3 id="单个插入" tabindex="-1">单个插入 <a class="header-anchor" href="#单个插入" aria-label="Permalink to &quot;单个插入&quot;">​</a></h3><p>使用insert() 或 save() 方法向集合中插入文档，语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.insertOne({</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">age</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">25</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">email</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">alice@example.com</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">isStudent</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: true,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">address</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">city</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">New York</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">state</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">NY</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">country</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">USA</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    },</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hobbies</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: [&quot;reading&quot;, &quot;traveling&quot;, &quot;swimming&quot;],</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">createdAt</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">updatedAt</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="批量插入" tabindex="-1">批量插入 <a class="header-anchor" href="#批量插入" aria-label="Permalink to &quot;批量插入&quot;">​</a></h3><blockquote><p><strong>如果某条数据插入失败，将会终止插入，但已经插入成功的数据不会回滚掉</strong></p></blockquote><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">  { name: &quot;Alice&quot;, age: 28 },</span></span>
<span class="line"><span style="color:#A6ACCD;">  { name: &quot;Bob&quot;, age: 35 },</span></span>
<span class="line"><span style="color:#A6ACCD;">  { name: &quot;Charlie&quot;, age: 42 }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><h3 id="插入失败处理" tabindex="-1">插入失败处理 <a class="header-anchor" href="#插入失败处理" aria-label="Permalink to &quot;插入失败处理&quot;">​</a></h3><p>因为批量插入由于数据较多容易出现失败，因此，可以使用try catch进行异常捕捉处理，测试的时候可以不处理。如（了解）：</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">try { </span></span>
<span class="line"><span style="color:#A6ACCD;">	db.comment.insertMany([ </span></span>
<span class="line"><span style="color:#A6ACCD;">		{&quot;_id&quot;:&quot;4&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;articleid&quot;:&quot;100001&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;content&quot;:&quot;专家说不能空腹吃饭，影响健康。&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;userid&quot;:&quot;1003&quot;,&quot;nickname&quot;:&quot;凯 撒&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;createdatetime&quot;:new Date(&quot;2019-08-06T08:18:35.288Z&quot;),</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;likenum&quot;:NumberInt(2000),</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;state&quot;:&quot;1&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">		}, </span></span>
<span class="line"><span style="color:#A6ACCD;">		{&quot;_id&quot;:&quot;5&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;articleid&quot;:&quot;100001&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;content&quot;:&quot;研究表明，刚烧开的水千万不能喝，因为烫嘴。&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;userid&quot;:&quot;1003&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;nickname&quot;:&quot;凯撒&quot;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;createdatetime&quot;:new Date(&quot;2019-08- 06T11:01:02.521Z&quot;),</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;likenum&quot;:NumberInt(3000),</span></span>
<span class="line"><span style="color:#A6ACCD;">		&quot;state&quot;:&quot;1&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">		} </span></span>
<span class="line"><span style="color:#A6ACCD;">	]); </span></span>
<span class="line"><span style="color:#A6ACCD;">} catch (e) { </span></span>
<span class="line"><span style="color:#A6ACCD;">	print (e); </span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h3 id="注意事项" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项" aria-label="Permalink to &quot;注意事项&quot;">​</a></h3><blockquote><p><strong>集合如果不存在，则会隐式创建</strong></p><p>mongo数字，<strong>默认情况下是double类型</strong>，<strong>如果要存整型，必须使用函数NumberInt(整型数字)</strong>，否则取就有问题</p><p><strong>插入当前日期使用 new Date()</strong></p><p><strong>插入的数据没有指定 _id ，会自动生成主键值</strong></p><p><strong>如果某字段没值，可以赋值为null，或不写该字段</strong></p></blockquote><blockquote><p>文档中的<strong>键值对是有序的</strong>。</p><p><strong>文档中的值不仅可以是在双引号里面的字符串，还可以是其他几种数据类型</strong>（甚至可以是整个嵌入的文档)。</p><p>MongoDB<strong>区分类型和大小写</strong>。</p><p>MongoDB的<strong>文档不能有重复的键</strong>。</p><p><strong>文档的键是字符串。除了少数例外情况，键可以使用任意UTF-8字符</strong></p></blockquote><h2 id="删除文档" tabindex="-1">删除文档 <a class="header-anchor" href="#删除文档" aria-label="Permalink to &quot;删除文档&quot;">​</a></h2><p>MongoDB通过collection对象的<code>remove、delete</code>方法来删除集合中的文档</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 单个删除</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">users</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">deleteOne</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">John Doe</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">})</span></span></code></pre></div><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 批量删除</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.deleteMany({&quot;age&quot;: {&quot;$gte&quot;: 30}})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.remove({&quot;name&quot;: &quot;Alice&quot;})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 以下语句可以将数据全部删除，请慎用 </span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.remove({})</span></span></code></pre></div><h2 id="更新文档⭐" tabindex="-1">更新文档⭐ <a class="header-anchor" href="#更新文档⭐" aria-label="Permalink to &quot;更新文档⭐&quot;">​</a></h2><p>更新时，如果没有该字段，就会增加该字段，更新文档的语法： 第一个字段是查询，第二个字段是修改</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.update(query, update, options)</span></span>
<span class="line"><span style="color:#A6ACCD;"># 或</span></span>
<span class="line"><span style="color:#A6ACCD;"># query：修改的查询条件，类似于SQL中的WHERE部分</span></span>
<span class="line"><span style="color:#A6ACCD;"># update：更新属性的操作符，类似与SQL中的SET部分</span></span>
<span class="line"><span style="color:#A6ACCD;"># multi：设置为true时会更新所有符合条件的文档，默认为false只更新找到的第一条</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.update(</span></span>
<span class="line"><span style="color:#A6ACCD;">	&lt;query&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">	&lt;update&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">	{</span></span>
<span class="line"><span style="color:#A6ACCD;">		upsert: &lt;boolean&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		multi: &lt;boolean&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		writeConcern: &lt;document&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		collation: &lt;document&gt;,</span></span>
<span class="line"><span style="color:#A6ACCD;">		arrayFilters: [ &lt;filterdocument1&gt;, ... ],</span></span>
<span class="line"><span style="color:#A6ACCD;">		hint: &lt;document|string&gt; // Available starting in MongoDB 4.2</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><h3 id="_1、覆盖的修改" tabindex="-1">1、覆盖的修改 <a class="header-anchor" href="#_1、覆盖的修改" aria-label="Permalink to &quot;1、覆盖的修改&quot;">​</a></h3><blockquote><p>覆盖修改，不好用，他会删除多余的字段，一般都使用局部修改</p></blockquote><p>如果我们想修改_id为1的记录，点赞量为1001，输入以下语句：</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.update(</span></span>
<span class="line"><span style="color:#A6ACCD;">	{articleid:&#39;10001&#39;},</span></span>
<span class="line"><span style="color:#A6ACCD;">	{likenum:NumberInt(1001)}</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>执行后，我们会发现，这条文档除了likenum字段其它字段都不见了</p><h3 id="_2、局部修改-重要" tabindex="-1">2、局部修改(重要) <a class="header-anchor" href="#_2、局部修改-重要" aria-label="Permalink to &quot;2、局部修改(重要)&quot;">​</a></h3><p>为了解决这个问题，我们需要使用修改器$set来实现，</p><p>命令如下：</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.update(</span></span>
<span class="line"><span style="color:#A6ACCD;">	{_id:&quot;1&quot;},</span></span>
<span class="line"><span style="color:#A6ACCD;">	{$set:{password:&quot;javac++&quot;,sort:22}}</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><p>将title为<code>MongoDB 教程</code>的所有文档的title修改为<code>MongoDB</code>；</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">update</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">MongoDB 教程</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">},{</span><span style="color:#A6ACCD;">$set:</span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">MongoDB</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">}},{</span><span style="color:#A6ACCD;">multi:true</span><span style="color:#89DDFF;">})</span></span></code></pre></div><h3 id="_3、批量的修改" tabindex="-1">3、批量的修改 <a class="header-anchor" href="#_3、批量的修改" aria-label="Permalink to &quot;3、批量的修改&quot;">​</a></h3><p>更新所有用户为 1003 的用户的昵称为 凯撒大帝 。</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 默认只修改第一条数据</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.update({userid:&quot;1003&quot;},{$set:{nickname:&quot;凯撒2&quot;}})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 修改所有符合条件的数据</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.update({userid:&quot;1003&quot;},{$set:{nickname:&quot;凯撒大帝&quot;}},{multi:true})</span></span></code></pre></div><p>提示：如果不加后面的参数，则只更新符合条件的第一条记录</p><h3 id="_4、列值增长的修改" tabindex="-1">4、列值增长的修改 <a class="header-anchor" href="#_4、列值增长的修改" aria-label="Permalink to &quot;4、列值增长的修改&quot;">​</a></h3><p>如果我们想<strong>实现对某列值在原有值的基础上进行增加或减少</strong>，可以使用 $inc 运算符来实现。</p><p>需求：<strong>对3号数据的点赞数，每次递增1</strong> ，注意：这时进行更新不会覆盖</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.update({_id:&quot;3&quot;},{$inc:{likenum:NumberInt(1)}})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.update({name:&quot;Alice&quot;},{$inc:{age:NumberInt(1)}})</span></span></code></pre></div><h2 id="查询文档" tabindex="-1">查询文档 <a class="header-anchor" href="#查询文档" aria-label="Permalink to &quot;查询文档&quot;">​</a></h2><h3 id="查询语法" tabindex="-1">查询语法 <a class="header-anchor" href="#查询语法" aria-label="Permalink to &quot;查询语法&quot;">​</a></h3><p>查询数据的语法格式如下</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># query：查询条件，类似于SQL中的WHERE部分</span></span>
<span class="line"><span style="color:#A6ACCD;"># projection：可选，使用投影操作符指定返回的键</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.find(&lt;query&gt;, [projection])</span></span></code></pre></div><h3 id="查询所有" tabindex="-1">查询所有 <a class="header-anchor" href="#查询所有" aria-label="Permalink to &quot;查询所有&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.find</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({})</span></span></code></pre></div><blockquote><p>这里你会发现每条文档会有一个叫**_id的字段**，<strong>这个相当于我们原来关系数据库中表的主键</strong>，<strong>当你在插入文档记录时没有指定该字段， MongoDB会自动创建，其类型是ObjectID类型</strong>。如果我们在插入文档记录时指定该字段也可以，其类型可以是ObjectID类型，也可以是MongoDB支持的任意类型。</p></blockquote><h3 id="条件查询" tabindex="-1">条件查询 <a class="header-anchor" href="#条件查询" aria-label="Permalink to &quot;条件查询&quot;">​</a></h3><blockquote><p>如果我想<strong>按一定条件来查询</strong>，只要在<strong>find()中添加参数即可，参数也是json格式</strong></p></blockquote><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 注意：数据类型必须对应，不然会查不到数据</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({age:26})</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({&quot;name&quot;: &quot;Alice&quot;})</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({age : 26,&quot;name&quot;: &quot;Alice&quot;})</span></span></code></pre></div><h3 id="查询单条" tabindex="-1">查询单条 <a class="header-anchor" href="#查询单条" aria-label="Permalink to &quot;查询单条&quot;">​</a></h3><blockquote><p>如果你<strong>只需要返回符合条件的第一条数据，我们可以使用findOne命令来实现</strong>，语法和find一样。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 如：查询用户编号是1003的记录，但只最多返回符合条件的第一条记录： </span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.findOne({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="查询指定字段" tabindex="-1">查询指定字段 <a class="header-anchor" href="#查询指定字段" aria-label="Permalink to &quot;查询指定字段&quot;">​</a></h3><blockquote><p>即投影查询，1表示显示该字段、0表示不显示该字段，如果要查询结果<strong>返回部分字段</strong>，则需要使用投影查询（不显示所有字段，只显示指定的字段）</p></blockquote><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 设置显示的字段</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({&quot;name&quot;: &quot;Alice&quot;},{name:1,address:1})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({},{name:1,address:1})</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;"># 设置不显示的字段</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({&quot;name&quot;: &quot;Alice&quot;},{name:0,address:0})</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;"># 不可以既设置1又设置0,这样会出错，只有_id才能进行同时设置0</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({},{name:1,address:1,_id:0})</span></span></code></pre></div><h3 id="统计查询" tabindex="-1">统计查询 <a class="header-anchor" href="#统计查询" aria-label="Permalink to &quot;统计查询&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 统计comment集合的所有的记录数</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.</span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">()</span></span>
<span class="line"><span style="color:#A6ACCD;"># 查询符合条件的个数</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.find({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}).</span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">db.users.</span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="分页查询" tabindex="-1">分页查询 <a class="header-anchor" href="#分页查询" aria-label="Permalink to &quot;分页查询&quot;">​</a></h3><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 如果你想返回指定条数的记录，可以在find方法后调用limit来返回结果(TopN)，默认值20</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find().limit(3)</span></span>
<span class="line"><span style="color:#A6ACCD;"># skip方法同样接受一个数字参数作为跳过的记录条数。（前N个不要）,默认值是0</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find().skip(3)</span></span>
<span class="line"><span style="color:#A6ACCD;"># 可以使用limit方法来读取指定数量的数据，使用skip方法来跳过指定数量的数据</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合名.find().limit(数字).skip(数字)</span></span></code></pre></div><blockquote><p>分页查询：需求：每页2个，第二页开始：跳过前两条数据，接着值显示3和4条数据</p></blockquote><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 第一页</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find().skip(0).limit(2)</span></span>
<span class="line"><span style="color:#A6ACCD;"># 第二页</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find().skip(2).limit(2)</span></span>
<span class="line"><span style="color:#A6ACCD;"># 第三页</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find().skip(4).limit(2)</span></span></code></pre></div><blockquote><p>需要注意的是，使用 <code>skip</code> 方法可能会导致查询性能下降，特别是在跳过大量数据时。因此，最好尽可能避免使用 <code>skip</code> 方法，或者使用其他方法来实现分页查询，例如使用 <code>ObjectId</code> 进行分页。</p></blockquote><h3 id="排序查询" tabindex="-1">排序查询 <a class="header-anchor" href="#排序查询" aria-label="Permalink to &quot;排序查询&quot;">​</a></h3><blockquote><p><strong>sort() 方法对数据进行排序</strong>，<strong>sort() 方法可以通过参数指定排序的字段</strong>，<strong>使用 1 和 -1 来指定排序的方式</strong></p></blockquote><p>其中 1 为升序排列，而 -1 是用 于降序排列。</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.集合名称.find().sort(排序方式)</span></span></code></pre></div><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 按照score字段进行降序排序</span></span>
<span class="line"><span style="color:#A6ACCD;">db.students.find().sort({score: -1})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 按照score字段降序排序，如果两个文档score字段相同，则按照name字段进行升序排序</span></span>
<span class="line"><span style="color:#A6ACCD;">db.students.find().sort({score: -1, name: 1})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 使用limit()方法限制返回的文档数量</span></span>
<span class="line"><span style="color:#A6ACCD;">db.students.find().sort({score: -1}).limit(10)</span></span></code></pre></div><blockquote><p>提示： skip(), limilt(), sort()三个放在一起执行的时候，执行的顺序是先 sort(), 然后是 skip()，最后是显示的 limit()，和命令编写顺序无关。</p></blockquote><h2 id="高级查询" tabindex="-1">高级查询 <a class="header-anchor" href="#高级查询" aria-label="Permalink to &quot;高级查询&quot;">​</a></h2><h3 id="正则查询" tabindex="-1">正则查询 <a class="header-anchor" href="#正则查询" aria-label="Permalink to &quot;正则查询&quot;">​</a></h3><p>MongoDB使用<code>$regex</code>操作符来设置匹配字符串的正则表达式，可以用来模糊查询，类似于SQL中的like操作；</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 例如查询title中包含</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">教程</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">的文档；</span></span>
<span class="line"><span style="color:#A6ACCD;">db.article.find({title:{$regex:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;"># 不区分大小写的模糊查询，使用</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">$options</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">操作符；</span></span>
<span class="line"><span style="color:#A6ACCD;">db.article.find({title:{$regex:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">elasticsearch</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,$options:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$i</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;"># MongoDB的模糊查询是通过正则表达式的方式实现的</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.find({field:</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">正则表达式</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合.find({字段:</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">正则表达式</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 我要查询评论内容包含“开水”的所有文档</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find({content:</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">开水</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 如果要查询评论的内容中以“专家”开头的</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find({content:</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">^专家</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="比较查询" tabindex="-1">比较查询 <a class="header-anchor" href="#比较查询" aria-label="Permalink to &quot;比较查询&quot;">​</a></h3><p>MongoDB中的条件操作符，通过与SQL语句的对比来了解下；</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5.13/202205261050248.png" alt="image-20220526105053146" style="zoom:67%;"><p>&lt;, &lt;=, &gt;, &gt;= 这个操作符也是很常用的</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.集合名称.find({ &quot;field&quot; : { $gt: value }}) // 大于: field &gt; value</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合名称.find({ &quot;field&quot; : { $lt: value }}) // 小于: field &lt; value</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合名称.find({ &quot;field&quot; : { $gte: value }}) // 大于等于: field &gt;= value</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合名称.find({ &quot;field&quot; : { $lte: value }}) // 小于等于: field &lt;= value</span></span>
<span class="line"><span style="color:#A6ACCD;">db.集合名称.find({ &quot;field&quot; : { $ne: value }}) // 不等于: field != value</span></span></code></pre></div><p>示例：查询评论点赞数量大于700的记录</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#A6ACCD;">likenum:</span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;">$gt:NumberInt(</span><span style="color:#F78C6C;">700</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">}})</span></span></code></pre></div><p>查询title为<code>MongoDB 教程</code>的所有文档；</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">MongoDB 教程</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">})</span></span></code></pre></div><p>查询likes大于50的所有文档；</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">likes</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;">$gt:</span><span style="color:#F78C6C;">50</span><span style="color:#89DDFF;">}})</span></span></code></pre></div><p>AND条件可以通过在<code>find()</code>方法传入多个键，以逗号隔开来实现，例如查询title为<code>MongoDB 教程</code>并且by为<code>Andy</code>的所有文档；</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">MongoDB 教程</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">by</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Andy</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">})</span></span></code></pre></div><p>OR条件可以通过使用<code>$or</code>操作符实现，例如查询title为<code>Redis 教程</code>或<code>MongoDB 教程</code>的所有文档；</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#A6ACCD;">$or:[</span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Redis 教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB 教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><p>AND 和 OR条件的联合使用，例如查询likes大于50，并且title为<code>Redis 教程</code>或者<code>&quot;MongoDB 教程</code>的所有文档。</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">article</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">likes</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;">$gt:</span><span style="color:#F78C6C;">50</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> $or: [</span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Redis 教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">title</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB 教程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><h3 id="包含查询" tabindex="-1">包含查询 <a class="header-anchor" href="#包含查询" aria-label="Permalink to &quot;包含查询&quot;">​</a></h3><p>包含使用$in操作符，不包含使用$nin操作符</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 查询评论的集合中userid字段包含1003或1004的文档</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find({userid:{$in:[&quot;1003&quot;,&quot;1004&quot;]}})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 查询评论集合中userid字段不包含1003和1004的文档</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find({userid:{$nin:[&quot;1003&quot;,&quot;1004&quot;]}})</span></span></code></pre></div><h3 id="条件查询-1" tabindex="-1">条件查询 <a class="header-anchor" href="#条件查询-1" aria-label="Permalink to &quot;条件查询&quot;">​</a></h3><p>我们如果需要查询同时满足两个以上条件，需要使用$and操作符将条件进行关联。（相 当于SQL的and） 格式为：</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">$and:[ { },{ },{ } ]</span></span></code></pre></div><p>示例：查询评论集合中likenum大于等于700 并且小于2000的文档：</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.find(</span></span>
<span class="line"><span style="color:#A6ACCD;">{$and:[</span></span>
<span class="line"><span style="color:#A6ACCD;">	{likenum:{$gte:NumberInt(700)}},</span></span>
<span class="line"><span style="color:#A6ACCD;">	{likenum:{$lt:NumberInt(2000)}}</span></span>
<span class="line"><span style="color:#A6ACCD;">	]</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>如果两个以上条件之间是或者的关系，我们使用 操作符进行关联，与前面 and的使用方式相同 格式为：</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">$or:[ { },{ },{ } ]</span></span></code></pre></div><p>示例：查询评论集合中userid为1003，或者点赞数小于1000的文档记录</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.find(</span></span>
<span class="line"><span style="color:#A6ACCD;">{$or:[ </span></span>
<span class="line"><span style="color:#A6ACCD;">    {userid:&quot;1003&quot;},</span></span>
<span class="line"><span style="color:#A6ACCD;">    {likenum:{$lt:1000}}</span></span>
<span class="line"><span style="color:#A6ACCD;">]})</span></span></code></pre></div><h1 id="索引" tabindex="-1">索引 <a class="header-anchor" href="#索引" aria-label="Permalink to &quot;索引&quot;">​</a></h1><h2 id="索引概述" tabindex="-1">索引概述 <a class="header-anchor" href="#索引概述" aria-label="Permalink to &quot;索引概述&quot;">​</a></h2><blockquote><p>索引支持在MongoDB中<strong>高效地执行查询</strong>。如果没有索引，MongoDB必须执行全集合扫描，即扫描集合中的每个文档，以选择与查询语句匹配的文档。这种扫描全集合的查询效率是非常低的，特别在处理大量的数据时，查询可以要花费几十秒甚至几分钟，这对网站的性能是非常致命的。</p></blockquote><h2 id="索引类型" tabindex="-1">索引类型 <a class="header-anchor" href="#索引类型" aria-label="Permalink to &quot;索引类型&quot;">​</a></h2><blockquote><ul><li>单键索引（single field index）：以单个字段为索引键的索引。</li><li>复合索引（compound index）：以多个字段为索引键的索引，可以提高复合查询的性能。</li><li>多键索引（multikey index）：用于数组字段的索引。</li><li>全文索引（text index）：用于全文搜索。</li><li>地理空间索引（geospatial index）：用于地理空间查询。</li></ul></blockquote><h2 id="索引管理" tabindex="-1">索引管理 <a class="header-anchor" href="#索引管理" aria-label="Permalink to &quot;索引管理&quot;">​</a></h2><h3 id="查看索引" tabindex="-1">查看索引 <a class="header-anchor" href="#查看索引" aria-label="Permalink to &quot;查看索引&quot;">​</a></h3><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 返回一个集合中的所有索引的数组</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.getIndexes()</span></span></code></pre></div><h3 id="创建索引" tabindex="-1">创建索引 <a class="header-anchor" href="#创建索引" aria-label="Permalink to &quot;创建索引&quot;">​</a></h3><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.createIndex(keys, options)</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 按升序创建索引</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.createIndex({userid:</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">})</span></span>
<span class="line"><span style="color:#A6ACCD;"># 对 userid 和 nickname 同时建立复合（Compound）索引</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.createIndex({userid:</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,nickname:</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="删除索引" tabindex="-1">删除索引 <a class="header-anchor" href="#删除索引" aria-label="Permalink to &quot;删除索引&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 指定索引的移除</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.dropIndex(indexName)</span></span>
<span class="line"><span style="color:#A6ACCD;"># 删除 comment 集合中 userid 字段上的升序索引</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.dropIndex({userid:</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 所有索引的移除，提示： _id 的字段的索引是无法删除的，只能删除非 _id 字段的索引</span></span>
<span class="line"><span style="color:#A6ACCD;">db.collection.dropIndexes()</span></span>
<span class="line"><span style="color:#A6ACCD;"># 删除集合中所有索引</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.dropIndexes()</span></span></code></pre></div><h3 id="分析查询性能" tabindex="-1">分析查询性能 <a class="header-anchor" href="#分析查询性能" aria-label="Permalink to &quot;分析查询性能&quot;">​</a></h3><blockquote><p>通常使用执行计划（解释计划、Explain Plan）来查看查询的情况，如查询耗费的时间、 否基于索引查询等。 那么，通常，我们想知道，建立的索引是否有效，效果如何，都需要通过执行计划查看。</p></blockquote><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.find(query,options).explain(options)</span></span></code></pre></div><p>查看根据userid查询数据的情况，生成结果中：<strong>关键点看： &quot;stage&quot; : &quot;COLLSCAN&quot;, 表示全集合扫描</strong></p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.comment.find({userid:&quot;1003&quot;}).explain()</span></span></code></pre></div><h2 id="地理索引" tabindex="-1">地理索引 <a class="header-anchor" href="#地理索引" aria-label="Permalink to &quot;地理索引&quot;">​</a></h2><p>MongoDB 支持地理位置数据的存储和查询，可以使用地理空间索引提高地理查询的性能。下面是使用示例：</p><p>假设有一个 <code>locations</code> 集合，其中包含了每个城市的经度和纬度：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.locations.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Shanghai</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, coordinates: [121.47, 31.23]}},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Beijing</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, coordinates: [116.40, 39.90]}},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Guangzhou</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, coordinates: [113.27, 23.13]}},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Shenzhen</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, coordinates: [114.05, 22.55]}}</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><p>接下来，可以在 <code>location</code> 字段上创建地理空间索引：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.locations.createIndex({</span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2dsphere</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>现在可以查询离某个坐标最近的城市。例如，查询距离经度为 121.47，纬度为 31.23 的坐标最近的城市：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.locations.findOne({</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $near: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      $</span><span style="color:#F78C6C;">geometry</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        coordinates: [121.47, 31.23]</span></span>
<span class="line"><span style="color:#A6ACCD;">      }</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>现在可以查询某个半径内的城市。例如，查询距离经度为 121.47，纬度为 31.23 的坐标半径为 200 公里内的城市：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.locations.find({</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">location</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $near: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      $</span><span style="color:#F78C6C;">geometry</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F78C6C;">type</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Point</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        coordinates: [121.47, 31.23]</span></span>
<span class="line"><span style="color:#A6ACCD;">      },</span></span>
<span class="line"><span style="color:#A6ACCD;">      $maxDistance: </span><span style="color:#F78C6C;">200000</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> 公里</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h2 id="全文检索" tabindex="-1">全文检索 <a class="header-anchor" href="#全文检索" aria-label="Permalink to &quot;全文检索&quot;">​</a></h2><p>MongoDB 4.2 及以上版本中引入了全文索引功能，可以通过 <code>$search</code> 管道操作符在全文索引上执行搜索。</p><p>假设有一个 <code>articles</code> 集合，其中包含了三篇文章</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.articles.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    title: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Introduction to MongoDB</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    content: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB is a popular NoSQL database.</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    title: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Getting started with MongoDB</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    content: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">To use MongoDB, you need to first install it and create a database.</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    title: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Advanced MongoDB techniques</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    content: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB supports advanced features such as aggregation pipelines and transactions.</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">]);</span></span></code></pre></div><p>接下来，对 <code>articles</code> 集合进行全文搜索，查找包含关键字 &quot;MongoDB&quot; 的文章</p><h3 id="创建全文索引" tabindex="-1">创建全文索引 <a class="header-anchor" href="#创建全文索引" aria-label="Permalink to &quot;创建全文索引&quot;">​</a></h3><p>首先，需要创建一个全文索引。假设有一个 <code>articles</code> 集合，需要在 <code>title</code> 和 <code>content</code> 字段上创建全文索引</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.articles.createIndex({ title: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">text</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">, content: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">text</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> });</span></span></code></pre></div><h3 id="查询测试" tabindex="-1">查询测试 <a class="header-anchor" href="#查询测试" aria-label="Permalink to &quot;查询测试&quot;">​</a></h3><p>然后，就可以使用 <code>$text</code> 操作符对集合进行全文搜索了。以下是一个示例，查询包含关键字 &quot;MongoDB&quot; 的所有文章</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.articles.find({</span></span>
<span class="line"><span style="color:#A6ACCD;">  $</span><span style="color:#C792EA;">text</span><span style="color:#A6ACCD;">: {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $search: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">});</span></span></code></pre></div><h3 id="删除全文索引" tabindex="-1">删除全文索引 <a class="header-anchor" href="#删除全文索引" aria-label="Permalink to &quot;删除全文索引&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 删除已存在的全文索引，可以使用 find 命令查找索引名：</span></span>
<span class="line"><span style="color:#A6ACCD;">db.articles.getIndexes</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;"># 通过以上命令获取索引名，本例的索引名为post_text_text，执行以下命令来删除索引：</span></span>
<span class="line"><span style="color:#A6ACCD;">db.articles.dropIndex(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">post_text_text</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><h2 id="实战案例" tabindex="-1">实战案例 <a class="header-anchor" href="#实战案例" aria-label="Permalink to &quot;实战案例&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Alice</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, age: </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Bob</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, age: </span><span style="color:#F78C6C;">25</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Charlie</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, age: </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Dave</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, age: </span><span style="color:#F78C6C;">35</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Eve</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, age: </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><p>这将向 <code>users</code> 集合插入 5 条文档，每个文档包含一个 <code>name</code> 字段和一个 <code>age</code> 字段。可以在 <code>users</code> 集合上创建一个 <code>age</code> 字段的升序索引，以提高按年龄查询的性能：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.createIndex({age: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>现在可以查询所有年龄大于 30 的用户，看看是否使用了索引：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.users.find({age: {$gt: </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">}}).explain</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>其中，<code>winningPlan</code> 字段表示 MongoDB 选择的最优执行计划，<code>usedIndex</code> 字段表示是否使用了索引。上述输出中的 <code>usedIndex</code> 值为 <code>true</code>，表示 MongoDB 使用了 <code>age</code> 字段的索引。</p><h1 id="聚合" tabindex="-1">聚合 <a class="header-anchor" href="#聚合" aria-label="Permalink to &quot;聚合&quot;">​</a></h1><h2 id="聚合语法" tabindex="-1">聚合语法 <a class="header-anchor" href="#聚合语法" aria-label="Permalink to &quot;聚合语法&quot;">​</a></h2><p><strong>基本语法</strong></p><p>MongoDB聚合操作可以将多个文档处理成一个结果集，类似于SQL中的GROUP BY操作。聚合操作可以对文档进行多种计算操作，如计数、求和、平均值等等。MongoDB聚合语法的基本结构如下</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">( [ { &lt;stage&gt; }, ... ] )</span></span></code></pre></div><blockquote><p>其中，db.collection.aggregate是聚合操作的基本语法格式，可以对指定的集合进行聚合操作。聚合操作由多个聚合阶段（stage）组成，每个聚合阶段都会对输入文档进行一定的转换处理，并将处理结果传递给下一个聚合阶段。聚合阶段的顺序可以任意调整，以满足具体的计算需求。</p></blockquote><p>下面是一些常用的聚合阶段及其用法：</p><p>$match：筛选符合条件的文档，相当于SQL中的WHERE子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $match: { </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">condition</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> } }</span></span></code></pre></div><p>$project：指定要返回的字段，并可以对字段进行一定的计算操作，相当于SQL中的SELECT子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $project: { </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">field1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">expression1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">field2</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">expression2</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">, ... } }</span></span></code></pre></div><p>$group：按照指定的字段对文档进行分组，并可以对分组后的文档进行一定的聚合计算，如计数、求和、平均值等等。相当于SQL中的GROUP BY子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $group: { _id: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">expression</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">field1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">: { </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">accumulator1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> : </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">expression1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> }, ... } }</span></span></code></pre></div><p>$sort：按照指定的字段对文档进行排序，相当于SQL中的ORDER BY子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $sort: { </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">field1</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">sort order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">field2</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">sort order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">, ... } }</span></span></code></pre></div><p>$limit：指定返回文档的数量限制，相当于SQL中的LIMIT子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $</span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">num</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> }</span></span></code></pre></div><p>$skip：指定跳过文档的数量，相当于SQL中的OFFSET子句。语法如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $</span><span style="color:#F78C6C;">skip</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">num</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> }</span></span></code></pre></div><p>$unwind：将数组类型的字段拆分为多个文档，并对拆分后的文档进行聚合计算，相当于SQL中的UNNEST操作。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">{ $unwind: </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F78C6C;">array</span><span style="color:#A6ACCD;"> field</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> }</span></span></code></pre></div><p>这些聚合阶段可以组合使用，形成更为复杂的聚合操作。例如，下面的聚合操作会对指定集合中的文档进行筛选、分组、计数，并按照计数结果进行排序：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">phpCopy codedb.collection.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">    { $match: { </span><span style="color:#F78C6C;">status</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> } },</span></span>
<span class="line"><span style="color:#A6ACCD;">    { $group: { _id: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$type</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span></code></pre></div><p><strong>常用阶段操作符</strong></p><table><thead><tr><th style="text-align:left;">操作符</th><th style="text-align:left;">简述</th></tr></thead><tbody><tr><td style="text-align:left;">$match</td><td style="text-align:left;">匹配操作符，用于对文档集合进行筛选</td></tr><tr><td style="text-align:left;">$project</td><td style="text-align:left;">投射操作符，用于重构每一个文档的字段，可以提取字段，重命名字段，甚至可以对原有字段进行操作后新增字段</td></tr><tr><td style="text-align:left;">$sort</td><td style="text-align:left;">排序操作符，用于根据一个或多个字段对文档进行排序</td></tr><tr><td style="text-align:left;">$limit</td><td style="text-align:left;">限制操作符，用于限制返回文档的数量</td></tr><tr><td style="text-align:left;">$skip</td><td style="text-align:left;">跳过操作符，用于跳过指定数量的文档</td></tr><tr><td style="text-align:left;">$count</td><td style="text-align:left;">统计操作符，用于统计文档的数量</td></tr><tr><td style="text-align:left;">$group</td><td style="text-align:left;">分组操作符，用于对文档集合进行分组</td></tr><tr><td style="text-align:left;">$unwind</td><td style="text-align:left;">拆分操作符，用于将数组中的每一个值拆分为单独的文档</td></tr><tr><td style="text-align:left;">$lookup</td><td style="text-align:left;">连接操作符，用于连接同一个数据库中另一个集合，并获取指定的文档，类似于 populate</td></tr></tbody></table><h2 id="实战示例" tabindex="-1">实战示例 <a class="header-anchor" href="#实战示例" aria-label="Permalink to &quot;实战示例&quot;">​</a></h2><p>假设有一个 <code>orders</code> 集合，其中包含了客户、订单日期和订单总额：</p><div class="language-sqlite"><button title="Copy Code" class="copy"></button><span class="lang">sqlite</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {customer: &#39;Alice&#39;, date: new Date(&#39;2022-01-01&#39;), total: 50},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {customer: &#39;Bob&#39;, date: new Date(&#39;2022-01-02&#39;), total: 100},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {customer: &#39;Charlie&#39;, date: new Date(&#39;2022-01-02&#39;), total: 150},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {customer: &#39;Alice&#39;, date: new Date(&#39;2022-01-03&#39;), total: 200},</span></span>
<span class="line"><span style="color:#A6ACCD;">  {customer: &#39;Charlie&#39;, date: new Date(&#39;2022-01-03&#39;), total: 250}</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><blockquote><p>可以使用聚合框架对该集合进行分组和汇总操作,将根据 <code>customer</code> 字段进行分组，并计算每个客户的订单总额和订单数。然后，按照订单总额进行排序，以便获取订单总额最高的客户</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $group: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$customer</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      total: { $sum: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$total</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> },</span></span>
<span class="line"><span style="color:#A6ACCD;">      count: { $sum: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $sort: { total: </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304222044045.png" alt="image-20230422204440949" style="zoom:80%;"><blockquote><p>聚合框架支持多种操作管道，包括投影、过滤、分组、排序、限制等等。可以根据实际需求，组合使用不同的操作管道，以实现各种复杂的查询需求。</p></blockquote><p>计算集合中某个字段的平均值、最大值和最小值</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $group: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      avgAmount: { $avg: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$amount</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> },</span></span>
<span class="line"><span style="color:#A6ACCD;">      maxAmount: { $max: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$amount</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> },</span></span>
<span class="line"><span style="color:#A6ACCD;">      minAmount: { $min: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$amount</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><p>根据字段进行分组，并计算每个分组中文档数量</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $group: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$status</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      count: { $sum: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><p>查找最高销售额的顾客姓名和销售额</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $group: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$customerName</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      totalAmount: { $sum: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$amount</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $sort: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      totalAmount: </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $</span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $project: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      customerName: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      totalAmount: </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><p>连接多个集合，查询顾客的订单和订单明细信息</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.customers.</span><span style="color:#F78C6C;">aggregate</span><span style="color:#A6ACCD;">([</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $lookup: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orders</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      localField: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      foreignField: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">customerId</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orders</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $unwind: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$orders</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $lookup: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orderDetails</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      localField: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orders._id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      foreignField: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orderId</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">orderDetails</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  },</span></span>
<span class="line"><span style="color:#A6ACCD;">  {</span></span>
<span class="line"><span style="color:#A6ACCD;">    $project: {</span></span>
<span class="line"><span style="color:#A6ACCD;">      _id: </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      customerName: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      orderId: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$orders._id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      orderDate: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$orders.date</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      productId: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$orderDetails.productId</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      quantity: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$orderDetails.quantity</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><h1 id="日志" tabindex="-1">日志 <a class="header-anchor" href="#日志" aria-label="Permalink to &quot;日志&quot;">​</a></h1><h2 id="慢日志" tabindex="-1">慢日志 <a class="header-anchor" href="#慢日志" aria-label="Permalink to &quot;慢日志&quot;">​</a></h2><p>可以在MongoDB的配置文件（通常是/etc/mongod.conf或/etc/mongodb.conf）中添加以下配置来启用慢查询日志：</p><h3 id="配置文件" tabindex="-1">配置文件 <a class="header-anchor" href="#配置文件" aria-label="Permalink to &quot;配置文件&quot;">​</a></h3><p>可以在MongoDB的配置文件（通常是/etc/mongod.conf或/etc/mongodb.conf）中添加以下配置来启用慢查询日志：</p><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">slowOpThresholdMs</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/var/log/mongodb/mongod.log</span></span></code></pre></div><h3 id="命令行配置" tabindex="-1">命令行配置 <a class="header-anchor" href="#命令行配置" aria-label="Permalink to &quot;命令行配置&quot;">​</a></h3><p>在启动MongoDB实例时，可以使用--slowms参数来记录慢查询日志。例如，以下命令将记录运行时间超过50毫秒的所有慢查询：在这个例子中，MongoDB将记录运行时间超过50毫秒的所有慢查询，并将它们写入标准输出（控制台）。您可以根据需要调整slowms参数的值。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#676E95;font-style:italic;">--profile=1 --slowms=200</span></span></code></pre></div><h1 id="集群" tabindex="-1">集群 <a class="header-anchor" href="#集群" aria-label="Permalink to &quot;集群&quot;">​</a></h1><h2 id="集群-1" tabindex="-1">集群 <a class="header-anchor" href="#集群-1" aria-label="Permalink to &quot;集群&quot;">​</a></h2><h3 id="创建集群并启动" tabindex="-1">创建集群并启动 <a class="header-anchor" href="#创建集群并启动" aria-label="Permalink to &quot;创建集群并启动&quot;">​</a></h3><p>1、准备数据目录</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node</span><span style="color:#89DDFF;">{</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">{</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">}</span></span></code></pre></div><p>或者删除文件夹,重新创建</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span></span></code></pre></div><p>2、启动这5个节点</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--replSet</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">rep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--dbpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/node1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">101</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--bind_ip</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0</span><span style="color:#C3E88D;">.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--logpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/log/log1/config.log</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--fork</span></span>
<span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--replSet</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">rep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--dbpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/node2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">102</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--bind_ip</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0</span><span style="color:#C3E88D;">.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--logpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/log/log2/config.log</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--fork</span></span>
<span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--replSet</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">rep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--dbpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/node3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">103</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--bind_ip</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0</span><span style="color:#C3E88D;">.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--logpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/log/log3/config.log</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--fork</span></span>
<span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--replSet</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">rep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--dbpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/node4</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">104</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--bind_ip</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0</span><span style="color:#C3E88D;">.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--logpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/log/log4/config.log</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--fork</span></span>
<span class="line"><span style="color:#FFCB6B;">mongod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--replSet</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">rep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--dbpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/node5</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--port</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">105</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--bind_ip</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0</span><span style="color:#C3E88D;">.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--logpath</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/data/mongodb/log/log5/config.log</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--fork</span></span></code></pre></div><h3 id="登录任意节点并初始化" tabindex="-1">登录任意节点并初始化 <a class="header-anchor" href="#登录任意节点并初始化" aria-label="Permalink to &quot;登录任意节点并初始化&quot;">​</a></h3><p>登录节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">101</span></span></code></pre></div><p>初始化</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">initiate</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myrs</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">members</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:[ </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:101</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:102</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:103</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">arbiterOnly</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> : true</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:104</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">,{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:105</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><p>查看状态</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">();</span></span></code></pre></div><h3 id="配置命令执行出错解决方式" tabindex="-1">配置命令执行出错解决方式 <a class="header-anchor" href="#配置命令执行出错解决方式" aria-label="Permalink to &quot;配置命令执行出错解决方式&quot;">​</a></h3><p>注意，执行出错，则要执行如下命令</p><p>官网地址：<a href="https://docs.mongodb.com/manual/reference/command/setDefaultRWConcern/" target="_blank" rel="noreferrer">setDefaultRWConcern — MongoDB Manual</a></p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.adminCommand({</span></span>
<span class="line"><span style="color:#A6ACCD;">  &quot;setDefaultRWConcern&quot; : 1,</span></span>
<span class="line"><span style="color:#A6ACCD;">  &quot;defaultWriteConcern&quot; : {</span></span>
<span class="line"><span style="color:#A6ACCD;">    &quot;w&quot; : 2</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="允许外网连接" tabindex="-1">允许外网连接 <a class="header-anchor" href="#允许外网连接" aria-label="Permalink to &quot;允许外网连接&quot;">​</a></h3><p>注意：端口号要是更改必须全部更改，如果集群如果有节点不更改则会出错</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">config = rs.conf()</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[0].host=&quot;101.43.33.227:101&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[1].host=&quot;101.43.33.227:102&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[2].host=&quot;101.43.33.227:103&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[3].host=&quot;101.43.33.227:104&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[4].host=&quot;101.43.33.227:105&quot;</span></span></code></pre></div><p>再执行</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.reconfig(config,{&quot;force&quot;:true})</span></span></code></pre></div><h3 id="进入主节点插入数据" tabindex="-1">进入主节点插入数据 <a class="header-anchor" href="#进入主节点插入数据" aria-label="Permalink to &quot;进入主节点插入数据&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.</span><span style="color:#F78C6C;">insert</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;"> {</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articleid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">100000</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">content</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">今天天气真好，阳光明媚</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">userid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">1001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">nickname</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Rose</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">createdatetime</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">likenum</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:NumberInt(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">state</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">null</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h3 id="查询测试-1" tabindex="-1">查询测试 <a class="header-anchor" href="#查询测试-1" aria-label="Permalink to &quot;查询测试&quot;">​</a></h3><p><strong>进入其他从节点都能查到数据</strong>，<strong>但先得设置从节点可读</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 登录从节点</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#676E95;font-style:italic;">--port=102</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 设置从节点可读</span></span>
<span class="line"><span style="color:#A6ACCD;">rs.secondaryOk</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 进行查看</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h3 id="主节点掉线测试" tabindex="-1">主节点掉线测试 <a class="header-anchor" href="#主节点掉线测试" aria-label="Permalink to &quot;主节点掉线测试&quot;">​</a></h3><p>首先查到主节点</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>查找并杀掉主节点</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ps </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">ef |grep mongod</span></span>
<span class="line"><span style="color:#F78C6C;">kill</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> port</span></span></code></pre></div><p>再次查看，看哪个节点变成了主节点,可以发现，<strong>已经成功选出主节点</strong></p><p>进行插入测试</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articleid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">100001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">content</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">今天天气不好，乌云密布</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">userid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">1002</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">nickname</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Jake</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">createdatetime</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:new Date()</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">likenum</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:NumberInt(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">state</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:null</span></span>
<span class="line"><span style="color:#89DDFF;">})</span></span></code></pre></div><p>其他从节点进行查询测试</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#676E95;font-style:italic;">--port=102</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.secondaryOk</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>曾经的主节点重连，查询数据看是否成功同步</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">replSet rep </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">dbpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node1 </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port </span><span style="color:#F78C6C;">101</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">bind_ip </span><span style="color:#F78C6C;">0.0.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">logpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log1</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">config</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">fork</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;"> 进入数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">101</span></span></code></pre></div><p>查询数据方式同上，<strong>可以发现数据已经成功同步</strong></p><h3 id="从节点掉线测试" tabindex="-1">从节点掉线测试 <a class="header-anchor" href="#从节点掉线测试" aria-label="Permalink to &quot;从节点掉线测试&quot;">​</a></h3><p>首先查到主节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>查找并杀掉端口号105从节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ps </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">ef </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">grep mongod</span></span>
<span class="line"><span style="color:#A6ACCD;">kill </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> port</span></span></code></pre></div><p>主节点中插入数据</p><p>进行插入测试</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articleid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">100003</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">content</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">今天天气不好号，乌云密布</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">userid</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">1003</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">nickname</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Jake</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">createdatetime</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:new Date()</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">likenum</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:NumberInt(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">state</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:null</span></span>
<span class="line"><span style="color:#89DDFF;">})</span></span></code></pre></div><p>再次启动该从节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">replSet rep </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">dbpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node5 </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port </span><span style="color:#F78C6C;">105</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">bind_ip </span><span style="color:#F78C6C;">0.0.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">logpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log5</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">config</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">fork</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#676E95;font-style:italic;">--port=105</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.secondaryOk</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db.comment.find</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>查询数据看是否成功同步</p><h3 id="节点优先级调整测试" tabindex="-1">节点优先级调整测试 <a class="header-anchor" href="#节点优先级调整测试" aria-label="Permalink to &quot;节点优先级调整测试&quot;">​</a></h3><p>在某些情况下，可能希望将一个数据中心中的成员选为主数据中心的成员，然后再选择其他数据中心的成员。您可以修改成员的<a href="https://docs.mongodb.com/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority" target="_blank" rel="noreferrer"><code>优先级</code></a>，以便一个数据中心中的成员的<a href="https://docs.mongodb.com/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority" target="_blank" rel="noreferrer"><code>优先级</code></a>高于其他数据中心中的成员。</p><p>副本集的某些成员（如具有网络限制或资源有限的成员）不应能够在故障转移中成为主<a href="https://docs.mongodb.com/manual/reference/glossary/#std-term-failover" target="_blank" rel="noreferrer">成员</a>。将不应成为主成员的成员配置为<a href="https://docs.mongodb.com/manual/core/replica-set-priority-0-member/#std-label-replica-set-secondary-only-members" target="_blank" rel="noreferrer">优先级为 0</a></p><p><strong>节点优先级设置为0，那么该节点永远不能成为主节点</strong></p><p>进入主节点，执行如下命令</p><p>注意：通过members[num]来设置对应成员</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">cfg </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">cfg</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">members</span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">].</span><span style="color:#A6ACCD;">priority </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span></span>
<span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">reconfig</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">cfg</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>稍等一会儿，102节点就会变成主节点了</p><h3 id="新增节点测试同步" tabindex="-1">新增节点测试同步 <a class="header-anchor" href="#新增节点测试同步" aria-label="Permalink to &quot;新增节点测试同步&quot;">​</a></h3><p>注意对应数字要改变，不要重复</p><p>第一步：在primary节点执行</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">101.43.33.227:106</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>第二步</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node6</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log6</span></span></code></pre></div><p>第三步</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">replSet rep </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">dbpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node6 </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port </span><span style="color:#F78C6C;">106</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">bind_ip </span><span style="color:#F78C6C;">0.0.0.0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">logpath </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log6</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">config</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">fork</span></span></code></pre></div><p>出现successful表示进入成功</p><p>进入该数据库</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">106</span></span></code></pre></div><p>设置该节点可读</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">secondaryOk</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>接下来进行查看数据是否同步</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show dbs</span></span>
<span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">find</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>测试，数据已经同步成功</p><h3 id="移除节点测试" tabindex="-1">移除节点测试 <a class="header-anchor" href="#移除节点测试" aria-label="Permalink to &quot;移除节点测试&quot;">​</a></h3><p>进入主节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">remove</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">101.43.33.227:106</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>查看集群状态</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>106节点成功移除</p><h3 id="无投票权成员测试" tabindex="-1">无投票权成员测试 <a class="header-anchor" href="#无投票权成员测试" aria-label="Permalink to &quot;无投票权成员测试&quot;">​</a></h3><p>一个集群最多有7个投票成员，因此需要设置一些节点为无投票成员</p><p>进入主节点后，选择配置一个节点为无投票成员</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">cfg </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">cfg</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">members</span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">].</span><span style="color:#A6ACCD;">votes </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#A6ACCD;">cfg</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">members</span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">].</span><span style="color:#A6ACCD;">priority </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">reconfig</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">cfg</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>进行查看是否成功</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">()</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220220204559631.png" alt="image-20220220204559631" style="zoom:67%;"><h3 id="主节点降级测试" tabindex="-1">主节点降级测试 <a class="header-anchor" href="#主节点降级测试" aria-label="Permalink to &quot;主节点降级测试&quot;">​</a></h3><p>指示副本集的主节点成为辅助。主节点退出后，其他节点开始进行选举。</p><p>该方法<strong>不会立即降低主数据库</strong>。如果没有<a href="https://docs.mongodb.com/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority" target="_blank" rel="noreferrer"><code>可选举的</code></a>辅助数据库与主数据库保持同步，则主数据库将等待最多（默认为 10 秒）以赶上辅助数据库。一旦有可选择的辅助数据库可用，该方法就会逐步降低主数据库。</p><p>一旦降级，原始主数据库将成为辅助数据库，并且在 指定的剩余时间内没有资格再次成为主数据库</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">stepDown</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">30</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>在Mongodb复制集主从切换的时候，使用<code>rs.stepDown(seconds)</code>，将当前主库实例“降级”，则<code>seconds</code>时间内，这个实例不会把自己选为primary角色。</p><p>通过执行&quot;rs.stepDown（30）&quot;命令将当前主库“降级”，30秒内这个实例不会把自己选为primary角色，相当于在30秒内此实例被“降级”了</p><p><strong>执行完这个命令，当有从节点追赶上主节点后，主节点立刻降级为从节点，同时会有其他从节点选举为主节点</strong></p><p><strong>30s之后，如果曾经主节点优先级比现任主节点优先级高的话，则会再次触发选举，曾经主节点可能再次成为主节点</strong></p><p>主要目的是方便主节点维护</p><h3 id="关闭mongodb节点" tabindex="-1">关闭mongodb节点 <a class="header-anchor" href="#关闭mongodb节点" aria-label="Permalink to &quot;关闭mongodb节点&quot;">​</a></h3><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod --shutdown --dbpath=/data/mongodb/node1</span></span></code></pre></div><h3 id="查看系统进程" tabindex="-1">查看系统进程 <a class="header-anchor" href="#查看系统进程" aria-label="Permalink to &quot;查看系统进程&quot;">​</a></h3><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ps -ef |grep mongod</span></span>
<span class="line"><span style="color:#A6ACCD;">kill -2 port</span></span></code></pre></div><h3 id="启动方式2" tabindex="-1">启动方式2 <a class="header-anchor" href="#启动方式2" aria-label="Permalink to &quot;启动方式2&quot;">​</a></h3><p>注意：因为配置了公网ip，所以确定有网才不会报错</p><p>网络图标消失解决： nmcli networking on</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir -p /data/mongodb/conf</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vi /data/mongodb/conf/node1.conf</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vi /data/mongodb/conf/node2.conf</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vi /data/mongodb/conf/node3.conf</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vi /data/mongodb/conf/node4.conf</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#MongoDB发送所有日志输出的目标指定为文件</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/data/mongodb/log/log1/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#当mongos或mongod实例重新启动时，mongos或mongod会将新条目附加到现有日志文件的末尾。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/data/mongodb/node1</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">#启用或禁用持久性日志以确保数据文件保持有效和可恢复。</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#启用在后台运行mongos或mongod进程的守护进程模式。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#指定用于保存mongos或mongod进程的进程ID的文件位置，其中mongos或mongod将写入其PID</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/data/mongodb/log/log1/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#服务实例绑定的IP，默认是localhost</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#bindIp</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#绑定的端口，默认是27017</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">101</span></span>
<span class="line"><span style="color:#F07178;">replication</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#副本集的名称</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">replSetName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myrs</span></span></code></pre></div><p>进行启动</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node1</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node2</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node3</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">node5</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f D:</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">mongoDB</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">node5</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">101</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">102</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">103</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">104</span></span>
<span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">105</span></span></code></pre></div><p>登录任意节点进行操作:登录任意节点进行初始化操作</p><p><strong>注意_id必须与–replSet 参数保持一致</strong></p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.initiate({&quot;_id&quot;:&quot;myrs&quot;,&quot;members&quot;:[ {&quot;_id&quot;:1,&quot;host&quot;:&quot;localhost:1001&quot;}, {&quot;_id&quot;:2,&quot;host&quot;:&quot;localhost:1002&quot;},{&quot;_id&quot;:3,&quot;host&quot;:&quot;localhost:1003&quot;}]})</span></span></code></pre></div><h2 id="日志-1" tabindex="-1">日志 <a class="header-anchor" href="#日志-1" aria-label="Permalink to &quot;日志&quot;">​</a></h2><p><a href="https://blog.csdn.net/chj_1224365967/article/details/115318523" target="_blank" rel="noreferrer">(38条消息) MongoDB如何查oplog.rs集合里的操作日志_Bertram的博客-CSDN博客_mongodb 查看oplog</a></p><h3 id="查看操作日志" tabindex="-1">查看操作日志 <a class="header-anchor" href="#查看操作日志" aria-label="Permalink to &quot;查看操作日志&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- ns：表示查询的数据库名.方法名</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">local</span></span>
<span class="line"><span style="color:#A6ACCD;">db.oplog.rs.find({ns:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">articledb.comment</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220312152447797.png" alt="image-20220312152447797" style="zoom:67%;"><h3 id="查看-oplog-的状态" tabindex="-1">查看 Oplog 的状态 <a class="header-anchor" href="#查看-oplog-的状态" aria-label="Permalink to &quot;查看 Oplog 的状态&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.printReplicationInfo</span><span style="color:#89DDFF;">()</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220314191739404.png" alt="image-20220314191739404" style="zoom:67%;"><h3 id="查看当前日志存储设置大小" tabindex="-1">查看当前日志存储设置大小 <a class="header-anchor" href="#查看当前日志存储设置大小" aria-label="Permalink to &quot;查看当前日志存储设置大小&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">local</span></span>
<span class="line"><span style="color:#A6ACCD;">db.oplog.rs.stats</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">.maxSize</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220314191846903.png" alt="image-20220314191846903" style="zoom:67%;"><p>查看 Oplog 最大大小和现在占用的大小，以及记录时长和时间</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">local</span></span>
<span class="line"><span style="color:#A6ACCD;">db.getReplicationInfo</span><span style="color:#89DDFF;">()</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220314191954464.png" alt="image-20220314191954464" style="zoom:67%;"><p>更改副本集成员的 Oplog 大小</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">local</span></span>
<span class="line"><span style="color:#A6ACCD;">db.adminCommand({replSetResizeOplog:</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,size:</span><span style="color:#F78C6C;">3000</span><span style="color:#A6ACCD;">})</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220314192031292.png" alt="image-20220314192031292" style="zoom:67%;"><h2 id="客户端连接" tabindex="-1">客户端连接 <a class="header-anchor" href="#客户端连接" aria-label="Permalink to &quot;客户端连接&quot;">​</a></h2><p>更改host(重要)</p><p><a href="https://blog.csdn.net/qq_18453581/article/details/120507908" target="_blank" rel="noreferrer">(39条消息) Mongodb副本集端口连接超时问题（修改副本集host）_Zakza的博客-CSDN博客</a></p><p>首先，进入primary节点</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.conf()</span></span></code></pre></div><p>注意看host值为localhost，这样使用SpringData是连接不上的，要进行修改</p><p>在primary节点，<strong>依次执行</strong>如下命名</p><p>注意：端口号要是更改必须全部更改，如果集群如果有节点不更改则会出错</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">config = rs.conf()</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[0].host=&quot;192.168.220.128:1001&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[1].host=&quot;192.168.220.128:1002&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[2].host=&quot;192.168.220.128:1003&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[3].host=&quot;192.168.220.128:1004&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[4].host=&quot;192.168.220.128:1005&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[0].host=&quot;101.43.33.227:101&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[1].host=&quot;101.43.33.227:102&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[2].host=&quot;101.43.33.227:103&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[3].host=&quot;101.43.33.227:104&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">config.members[4].host=&quot;101.43.33.227:105&quot;</span></span></code></pre></div><p>再执行</p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs.reconfig(config,{&quot;force&quot;:true})</span></span></code></pre></div><p>注意，执行出错，则要执行如下命令</p><p>官网地址：<a href="https://docs.mongodb.com/manual/reference/command/setDefaultRWConcern/" target="_blank" rel="noreferrer">setDefaultRWConcern — MongoDB Manual</a></p><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.adminCommand({</span></span>
<span class="line"><span style="color:#A6ACCD;">  &quot;setDefaultRWConcern&quot; : 1,</span></span>
<span class="line"><span style="color:#A6ACCD;">  &quot;defaultWriteConcern&quot; : {</span></span>
<span class="line"><span style="color:#A6ACCD;">    &quot;w&quot; : 2</span></span>
<span class="line"><span style="color:#A6ACCD;">  }</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><p>在外面，进行连接测试</p><p>进行连接</p><p>只需要修改application.yaml即可</p><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">spring</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#数据源配置</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">data</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">mongodb</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">uri</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mongodb://192.168.220.128:1001,192.168.220.128:1002,192.168.220.128:1003/articledb?connect=replicaSet?slaveOk=true&amp;replicaSet=nod</span></span></code></pre></div><p>进行测试时查询所有即可发现测试成功(前面配置了SpringData的环境)</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">//查询所有数据</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testFindAll</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Comment</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> commentService</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">findCommentList</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">    System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h1 id="分片集群" tabindex="-1">分片集群 <a class="header-anchor" href="#分片集群" aria-label="Permalink to &quot;分片集群&quot;">​</a></h1><h2 id="概念" tabindex="-1">概念 <a class="header-anchor" href="#概念" aria-label="Permalink to &quot;概念&quot;">​</a></h2><p>分片（sharding）是一种<strong>跨多台机器分布数据</strong>的方法， MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作的部署。</p><p><strong>换句话说：分片(sharding)是指将数据拆分，将其分散存在不同的机器上的过程</strong>。有时也用分区 (partitioning)来表示这个概念。</p><p><strong>将数据分散到不同的机器上，不需要功能强大的大型计算机就可以储存更多的数据，处理更多的负载</strong>。 具有大型数据集或高吞吐量应用程序的数据库系统可以会挑战单个服务器的容量。例如，高查询率会耗 尽服务器的CPU容量。工作集大小大于系统的RAM会强调磁盘驱动器的I/O容量。</p><p>有两种解决系统增长的方法：<strong>垂直扩展和水平扩展</strong>。</p><p><strong>垂直扩展</strong>意味着<strong>增加单个服务器的容量</strong>，例如使用更强大的CPU，添加更多RAM或增加存储空间量。可 用技术的局限性可能会限制单个机器对于给定工作负载而言足够强大。此外，基于云的提供商基于可用 的硬件配置具有硬性上限。结果，垂直缩放有实际的最大值。</p><p><strong>水平扩展</strong>意味着<strong>划分系统数据集并加载多个服务器，添加其他服务器以根据需要增加容量</strong>。虽然单个机器的总体速度或容量可能不高，但每台机器处理整个工作负载的子集，可能提供比单个高速大容量服务 器更高的效率。扩展部署容量只需要根据需要添加额外的服务器，这可能比单个机器的高端硬件的总体 成本更低。权衡是基础架构和部署维护的复杂性增加。<strong>MongoDB支持通过分片进行水平扩展</strong>。</p><h2 id="分片集群组件" tabindex="-1">分片集群组件 <a class="header-anchor" href="#分片集群组件" aria-label="Permalink to &quot;分片集群组件&quot;">​</a></h2><p>MongoDB分片群集包含以下组件：</p><ul><li><strong>分片</strong>（存储）：每个分片包含分片数据的子集。 每个分片都可以部署为副本集。</li><li><strong>mongos</strong>（路由）：mongos充当查询路由器，在客户端应用程序和分片集群之间提供接口。</li><li><strong>config servers</strong>（“调度”的配置）：配置服务器存储群集的元数据和配置设置。 从MongoDB 3.4开 始，必须将配置服务器部署为副本集（CSRS）。</li></ul><p>下图描述了分片集群中组件的交互</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220115174432575.png" alt="image-20220115174432575" style="zoom:80%;"><p>MongoDB在集合级别对数据进行分片，将集合数据分布在集群中的分片上。</p><h2 id="分片集群架构目标" tabindex="-1">分片集群架构目标 <a class="header-anchor" href="#分片集群架构目标" aria-label="Permalink to &quot;分片集群架构目标&quot;">​</a></h2><p>两个分片节点副本集（3+3）+一个配置节点副本集（3）+两个路由节点（2），共11个服务节点</p><h4 id="创建第一个分片节点" tabindex="-1">创建第一个分片节点 <a class="header-anchor" href="#创建第一个分片节点" aria-label="Permalink to &quot;创建第一个分片节点&quot;">​</a></h4><h5 id="创建文件夹" tabindex="-1">创建文件夹 <a class="header-anchor" href="#创建文件夹" aria-label="Permalink to &quot;创建文件夹&quot;">​</a></h5><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/data/db \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/data/db \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27218/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myshardrs01_27218/data/db</span></span></code></pre></div><h5 id="创建配置文件" tabindex="-1">创建配置文件 <a class="header-anchor" href="#创建配置文件" aria-label="Permalink to &quot;创建配置文件&quot;">​</a></h5><div class="language-mariadb"><button title="Copy Code" class="copy"></button><span class="lang">mariadb</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf</span></span></code></pre></div><h5 id="配置文件详情" tabindex="-1">配置文件详情 <a class="header-anchor" href="#配置文件详情" aria-label="Permalink to &quot;配置文件详情&quot;">​</a></h5><p>注意：日志不是文件夹，日志是文件config.log，写成文件夹启动会出错</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#MongoDB发送所有日志输出的目标指定为文件</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27018/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#当mongos或mongod实例重新启动时，mongos或mongod会将新条目附加到现有日志文件的末尾。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27018/data/db</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">#启用或禁用持久性日志以确保数据文件保持有效和可恢复。</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#启用在后台运行mongos或mongod进程的守护进程模式。</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#指定用于保存mongos或mongod进程的进程ID的文件位置，其中mongos或mongod将写入其PID</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#服务实例绑定的IP，默认是localhost</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#bindIp</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#绑定的端口，默认是27017</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27018</span></span>
<span class="line"><span style="color:#F07178;">replication</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#副本集的名称</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">replSetName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myshardrs01</span></span>
<span class="line"><span style="color:#F07178;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">#分片角色</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">clusterRole</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shardsvr</span></span></code></pre></div><h5 id="配置文件-1" tabindex="-1">配置文件 <a class="header-anchor" href="#配置文件-1" aria-label="Permalink to &quot;配置文件&quot;">​</a></h5><p>其他两个只需要修改下面这4个27018即可</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27218/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27218/data/db</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs01_27218/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27218</span></span>
<span class="line"><span style="color:#F07178;">replication</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">replSetName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myshardrs01</span></span>
<span class="line"><span style="color:#F07178;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">clusterRole</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shardsvr</span></span></code></pre></div><p>依次启动服务</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27018</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27118</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27218</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><p>任意进入一个节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27018</span></span></code></pre></div><p>进行初始化操作</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">initiate</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myshardrs01</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">members</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:[ </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27018</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27118</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27218</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">arbiterOnly</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> : true</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><h4 id="创建第二个分片节点" tabindex="-1">创建第二个分片节点 <a class="header-anchor" href="#创建第二个分片节点" aria-label="Permalink to &quot;创建第二个分片节点&quot;">​</a></h4><p><strong>内容同第一个分片节点，只是端口号不同</strong></p><h5 id="创建文件夹-1" tabindex="-1">创建文件夹 <a class="header-anchor" href="#创建文件夹-1" aria-label="Permalink to &quot;创建文件夹&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27318</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27318</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27418</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27418</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27518</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27518</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span></span></code></pre></div><h5 id="创建配置文件-1" tabindex="-1">创建配置文件 <a class="header-anchor" href="#创建配置文件-1" aria-label="Permalink to &quot;创建配置文件&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27318</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27418</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27518</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs02_27318/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs02_27318/data/db</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myshardrs02_27318/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27318</span></span>
<span class="line"><span style="color:#F07178;">replication</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">replSetName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myshardrs02</span></span>
<span class="line"><span style="color:#F07178;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">clusterRole</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shardsvr</span></span></code></pre></div><h5 id="依次启动服务" tabindex="-1">依次启动服务 <a class="header-anchor" href="#依次启动服务" aria-label="Permalink to &quot;依次启动服务&quot;">​</a></h5><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod -f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod -f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod -f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf</span></span></code></pre></div><h5 id="进入和初始化节点" tabindex="-1">进入和初始化节点 <a class="header-anchor" href="#进入和初始化节点" aria-label="Permalink to &quot;进入和初始化节点&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27318</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">initiate</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myshardrs02</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">members</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:[ </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27318</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27418</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27518</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">arbiterOnly</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> : true</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><h4 id="配置节点副本集" tabindex="-1">配置节点副本集 <a class="header-anchor" href="#配置节点副本集" aria-label="Permalink to &quot;配置节点副本集&quot;">​</a></h4><h5 id="创建文件夹-2" tabindex="-1">创建文件夹 <a class="header-anchor" href="#创建文件夹-2" aria-label="Permalink to &quot;创建文件夹&quot;">​</a></h5><div class="language-makefile"><button title="Copy Code" class="copy"></button><span class="lang">makefile</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#建立数据节点data和日志目录</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/data/db \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/data/db \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/log \ &amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/data/db</span></span></code></pre></div><h5 id="创建配置文件-2" tabindex="-1">创建配置文件 <a class="header-anchor" href="#创建配置文件-2" aria-label="Permalink to &quot;创建配置文件&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27019</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27119</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27219</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myconfigrs_27019/log/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">storage</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dbPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myconfigrs_27019/data/db</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">journal</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">enabled</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27019</span></span>
<span class="line"><span style="color:#F07178;">replication</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">replSetName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myconfigrs</span></span>
<span class="line"><span style="color:#F07178;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">clusterRole</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">configsvr</span></span></code></pre></div><h5 id="依次启动服务-1" tabindex="-1">依次启动服务 <a class="header-anchor" href="#依次启动服务-1" aria-label="Permalink to &quot;依次启动服务&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27019</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27119</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span>
<span class="line"><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27219</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><h5 id="进入和初始化节点-1" tabindex="-1">进入和初始化节点 <a class="header-anchor" href="#进入和初始化节点-1" aria-label="Permalink to &quot;进入和初始化节点&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27019</span></span></code></pre></div><p>此时没有仲裁节点</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rs</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">initiate</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myconfigrs</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">members</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:[ </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27019</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27119</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">},</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">host</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">localhost:27219</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">})</span></span></code></pre></div><h4 id="创建路由节点" tabindex="-1">创建路由节点 <a class="header-anchor" href="#创建路由节点" aria-label="Permalink to &quot;创建路由节点&quot;">​</a></h4><h5 id="创建第一个路由节点" tabindex="-1">创建第一个路由节点 <a class="header-anchor" href="#创建第一个路由节点" aria-label="Permalink to &quot;创建第一个路由节点&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span></span>
<span class="line"><span style="color:#A6ACCD;">vi </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongos</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> file</span></span>
<span class="line"><span style="color:#A6ACCD;">  path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/mymongos_27017/log/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> true</span></span>
<span class="line"><span style="color:#A6ACCD;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> true</span></span>
<span class="line"><span style="color:#A6ACCD;">  pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/mymongos_27017/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#A6ACCD;">  port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27017</span></span>
<span class="line"><span style="color:#A6ACCD;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">指定配置节点副本集</span></span>
<span class="line"><span style="color:#A6ACCD;">  configDB</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> myconfigrs</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">27019</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">27119</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">27219</span></span></code></pre></div><p>启动和进入</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongos </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f  </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongos</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27017</span></span></code></pre></div><h4 id="路由节点上配置" tabindex="-1">路由节点上配置 <a class="header-anchor" href="#路由节点上配置" aria-label="Permalink to &quot;路由节点上配置&quot;">​</a></h4><p>分别将副本节点加进入</p><h5 id="添加分片" tabindex="-1">添加分片 <a class="header-anchor" href="#添加分片" aria-label="Permalink to &quot;添加分片&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">addShard</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">IP:Port</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">addShard</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myshardrs01/localhost:27018,localhost:27118,localhost:27218</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">addShard</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myshardrs02/localhost:27318,localhost:27418,localhost:27518</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span></code></pre></div><h5 id="查看状态" tabindex="-1">查看状态 <a class="header-anchor" href="#查看状态" aria-label="Permalink to &quot;查看状态&quot;">​</a></h5><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h5 id="移除分片" tabindex="-1">移除分片 <a class="header-anchor" href="#移除分片" aria-label="Permalink to &quot;移除分片&quot;">​</a></h5><p>提示：如果添加分片失败，需要先手动移除分片，检查添加分片的信息的正确性后，再次添加分片。</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use admin</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">runCommand</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> removeShard: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">myshardrs02</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>注意：如果只剩下最后一个shard，是无法删除的，移除时会自动转移分片数据，需要一个时间过程。完成后，再次执行删除分片命令才能真正删除。</p><h5 id="开启分片功能" tabindex="-1">开启分片功能 <a class="header-anchor" href="#开启分片功能" aria-label="Permalink to &quot;开启分片功能&quot;">​</a></h5><p>开启分片功能：sh.enableSharding(&quot;库名&quot;)、sh.shardCollection(&quot;库名.集合名&quot;,{&quot;key&quot;:1}) 在mongos上的articledb数据库配置sharding:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#F78C6C;">.enableSharding</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articledb</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h5 id="集合分片" tabindex="-1">集合分片 <a class="header-anchor" href="#集合分片" aria-label="Permalink to &quot;集合分片&quot;">​</a></h5><p>对集合分片，你必须使用 sh.shardCollection() 方法指定集合和分片键</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">shardCollection</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">namespace</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> key</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> unique</span><span style="color:#89DDFF;">)</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220311160108243.png" alt="image-20220311160108243" style="zoom:67%;"><p>对集合进行分片时,你需要选择一个片键（Shard Key） , shard key 是每条记录都必须包含的,且建立了</p><p>索引的单个字段或复合字段,MongoDB按照片键将数据划分到不同的 数据块 中,并将 数据块 均衡地分布</p><p>到所有分片中.为了按照片键划分数据块,MongoDB使用 基于哈希的分片方式（随机平均分配）或者基</p><p>于范围的分片方式（数值大小分配） 。用什么字段当片键都可以，如：nickname作为片键，但一定是必填字段。</p><p>分片规则一：哈希策略(推荐)</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">shardCollection</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articledb.comment</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">nickname</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hashed</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">})</span></span>
<span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">status</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>分片规则二：范围策略</p><p>对于基于范围的分片 ,MongoDB按照片键的范围把数据分成不同部分.假设有一个数字的片键:想象一个</p><p>从负无穷到正无穷的直线,每一个片键的值都在直线上画了一个点.MongoDB把这条直线划分为更短的不</p><p>重叠的片段,并称之为 数据块 ,每个数据块包含了片键在一定范围内的数据.</p><p>在使用片键做范围划分的系统中,拥有”相近”片键的文档很可能存储在同一个数据块中,因此也会存储在同</p><p>一个分片中. <strong>如使用作者年龄字段作为片键，按照点赞数的值进行分片</strong></p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">shardCollection</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">articledb.author</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">age</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">})</span></span></code></pre></div><p>注意的是： 1）一个集合只能指定一个片键，否则报错。 2）一旦对一个集合分片，分片键和分片值就不可改变。 如：不能给集合选择不同的分片键、不能更新 分片键的值。 3）根据age索引进行分配数据。</p><h5 id="查看分片状态" tabindex="-1">查看分片状态 <a class="header-anchor" href="#查看分片状态" aria-label="Permalink to &quot;查看分片状态&quot;">​</a></h5><p>显示集群的详细信息</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.printShardingStatus()</span></span></code></pre></div><p>查看均衡器是否工作（需要重新均衡时系统才会自动启动，不用管它）：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">isBalancerRunning</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>查看当前Balancer状态</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">getBalancerState</span><span style="color:#89DDFF;">()</span></span></code></pre></div><h4 id="插入数据测试" tabindex="-1">插入数据测试 <a class="header-anchor" href="#插入数据测试" aria-label="Permalink to &quot;插入数据测试&quot;">​</a></h4><p>测试一（哈希规则）：登录mongs后，向comment循环插入1000条数据做测试：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27017</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use articledb</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">var i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#F78C6C;">1000</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">++){</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">insert(</span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;">_id:i</span><span style="color:#89DDFF;">+</span><span style="color:#89DDFF;">&quot;&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">nickname:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">BoBo</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">count</span><span style="color:#89DDFF;">()</span></span></code></pre></div><p>提示：js的语法，因为mongo的shell是一个JavaScript的shell。</p><p>注意：从路由上插入的数据，必须包含片键，否则无法插入。</p><p>分别登陆两个片的主节点，统计文档数量</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27018</span></span>
<span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">count</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">506</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">27318</span></span>
<span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">comment</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">count</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">494</span></span></code></pre></div><p>可以看到，1000条数据近似均匀的分布到了2个shard上。是根据片键的哈希值分配的。这种分配方式非</p><p>常易于水平扩展：一旦数据存储需要更大空间，可以直接再增加分片即可，同时提升了性能。</p><p>使用db.comment.stats()查看单个集合的完整情况，mongos执行该命令可以查看该集合的数据分片的</p><p>情况。使用sh.status()查看本库内所有集合的分片信息。</p><p>测试二（范围规则）：登录mongs后，向comment循环插入1000条数据做测试：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use articledb</span></span>
<span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">var i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#F78C6C;">20000</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">++){</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">author</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">save(</span><span style="color:#89DDFF;">{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">BoBBBoBo</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">age</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">:NumberInt(i%</span><span style="color:#F78C6C;">120</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">}</span></span></code></pre></div><p>如果查看状态发现没有分片，则可能是由于以下原因造成了</p><ul><li><p>系统繁忙，正在分片中。</p></li><li><p>数据块（chunk）没有填满，默认的数据块尺寸（chunksize）是64M，填满后才会考虑向其他片的数据块填充数据，因此，为了测试，可以将其改小，这里改为1M，操作如下：</p></li></ul><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">use config</span></span>
<span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">settings</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">save</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> _id:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">chunksize</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> value: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>测试完改回来</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">settings</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">save</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> _id:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">chunksize</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> value: </span><span style="color:#F78C6C;">64</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>注意：要先改小，再设置分片。为了测试，可以先删除集合，重新建立集合的分片策略，再插入数据测 试即可。</p><h4 id="再增加一个路由节点" tabindex="-1">再增加一个路由节点 <a class="header-anchor" href="#再增加一个路由节点" aria-label="Permalink to &quot;再增加一个路由节点&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27117</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">log</span></span>
<span class="line"><span style="color:#A6ACCD;">vi </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27117</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongos</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">conf</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">systemLog</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">destination</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">path</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/mymongos_27117/log/config.log</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">logAppend</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#F07178;">processManagement</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">fork</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">pidFilePath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/mongodb/sharded_cluster/mymongos_27117/log/mongod.pid</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#F07178;">net</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindIp</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.0.0.0</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">27117</span></span>
<span class="line"><span style="color:#F07178;">sharding</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#指定配置节点副本集</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">configDB</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">myconfigrs/127.0.0.1:27019,127.0.0.1:27119,127.0.0.1:27219</span></span></code></pre></div><p>连接启动</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongos -f /mongodb/sharded_cluster/mymongos_27117/mongos.conf</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mongo --port=27117</span></span></code></pre></div><p>使用mongo客户端登录27117，发现，<strong>第二个路由无需配置</strong>，因为分片配置都保存到了配置服务器中</p><p>外部连接只需要连接路由节点即可</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">192.168.220.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">27017</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220311163240541.png" alt="image-20220311163240541" style="zoom:67%;"><p>SpringDataMongDB连接分片集群 Java客户端常用的是SpringDataMongoDB，其连接的是mongs路由，配置和单机mongod的配置是一 样的。 多个路由的时候的SpringDataMongoDB的客户端配置参考如下：</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">spring</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#数据源配置</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">data</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">mongodb</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">#连接路由字符串</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">uri</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mongodb://180.76.159.126:27017,180.76.159.126:27117/articledb</span></span></code></pre></div><p>通过日志发现，写入数据的时候，会选择一个路由写入</p><h2 id="出错处理方案" tabindex="-1">出错处理方案 <a class="header-anchor" href="#出错处理方案" aria-label="Permalink to &quot;出错处理方案&quot;">​</a></h2><p>如果在搭建分片的时候有操作失败或配置有问题，需要重新来过的，可以进行如下操作： 第一步：查询出所有的测试服务节点的进程：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ps </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">ef </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">grep mongo</span></span></code></pre></div><p>根据上述的进程编号，依次中断进程</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">kill </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> 进程编号</span></span></code></pre></div><p>第二步：清除所有的节点的数据</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27019</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27119</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myconfigrs_27219</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27018</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27118</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs01_27218</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27318</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27418</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">myshardrs02_27518</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;</span></span>
<span class="line"><span style="color:#A6ACCD;">rm </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">rf </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27117</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">data</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">db</span><span style="color:#89DDFF;">/*.*</span></span></code></pre></div><p>第三步：查看或修改有问题的配置</p><p>第四步：依次启动所有节点，不包括路由节点</p><p>第五步：对两个数据分片副本集和一个配置副本集进行初始化和相关配置</p><p>第六步：检查路由mongos的配置，并启动mongos</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bin</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f</span></span>
<span class="line"><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongos</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cfg</span></span>
<span class="line"><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bin</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongod </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">f</span></span>
<span class="line"><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongodb</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sharded_cluster</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mymongos_27017</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">mongos</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cfg</span></span></code></pre></div><p>第七步：mongo登录mongos，在其上进行相关操作</p><h1 id="map-reduce" tabindex="-1">Map Reduce <a class="header-anchor" href="#map-reduce" aria-label="Permalink to &quot;Map Reduce&quot;">​</a></h1><blockquote><p>Map-Reduce是一种计算模型，简单的说就是将大批量的工作（数据）分解（MAP）执行，然后再将结果合并成最终结果（REDUCE）。MongoDB提供的Map-Reduce非常灵活，对于大规模数据分析也相当实用。</p></blockquote><h2 id="基本语法" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法" aria-label="Permalink to &quot;基本语法&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.collection.mapReduce(</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#F78C6C;">function</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> {emit(</span><span style="color:#F78C6C;">key</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">value</span><span style="color:#A6ACCD;">);},  </span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">map 函数</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#F78C6C;">function</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">key</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">) {</span><span style="color:#F78C6C;">return</span><span style="color:#A6ACCD;"> reduceFunction},   </span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">reduce 函数</span></span>
<span class="line"><span style="color:#A6ACCD;">   {</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">out</span><span style="color:#A6ACCD;">: </span><span style="color:#F78C6C;">collection</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">      query: document,</span></span>
<span class="line"><span style="color:#A6ACCD;">      sort: document,</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;">: </span><span style="color:#C792EA;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">   }</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><blockquote><p>使用 MapReduce 要实现两个函数 Map 函数和 Reduce 函数,Map 函数调用 emit(key, value), 遍历 collection 中所有的记录, 将 key 与 value 传递给 Reduce 函数进行处理。Map 函数必须调用 emit(key, value) 返回键值对。</p></blockquote><p>参数说明</p><ul><li><strong>map</strong> ：映射函数 (生成键值对序列,作为 reduce 函数参数)。</li><li><strong>reduce</strong> 统计函数，reduce函数的任务就是将key-values变成key-value，就是把values数组变成一个单一值</li><li><strong>out</strong> 统计结果存放集合 (不指定则使用临时集合,在客户端断开后自动删除)。</li><li><strong>query</strong> 一个筛选条件，只有满足条件的文档才会调用map函数。（query。limit，sort可以随意组合）</li><li><strong>sort</strong> 和limit结合的sort排序参数（也是在发往map函数前给文档排序），可以优化分组机制</li><li><strong>limit</strong> 发往map函数的文档数量的上限（要是没有limit，单独使用sort的用处不大）</li></ul><h2 id="图解实例" tabindex="-1">图解实例 <a class="header-anchor" href="#图解实例" aria-label="Permalink to &quot;图解实例&quot;">​</a></h2><p>以下实例在集合 orders 中查找 status:&quot;A&quot; 的数据，并根据 cust_id 来分组，并计算 amount 的总和。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221654464.png" alt="image-20230422165411375" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221654381.png" alt="image-20230422165427279" style="zoom:80%;"><h2 id="实战使用" tabindex="-1">实战使用 <a class="header-anchor" href="#实战使用" aria-label="Permalink to &quot;实战使用&quot;">​</a></h2><h3 id="数据准备" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备" aria-label="Permalink to &quot;数据准备&quot;">​</a></h3><blockquote><p>这个数据集合包含了5个订单，其中user_id为1、2、3的三个用户分别有2、2和1个订单，每个订单中包含了多个商品项。我们可以使用上面的MapReduce操作对这些订单数据进行聚合处理和分析。</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">db.orders.insertMany([</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">user_id:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">total:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100.0</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">items:</span><span style="color:#A6ACCD;"> [</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product B</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product C</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        ]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">user_id:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">total:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50.0</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">items:</span><span style="color:#A6ACCD;"> [</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product B</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        ]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">user_id:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">total:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">75.0</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">items:</span><span style="color:#A6ACCD;"> [</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product C</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        ]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">user_id:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">total:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">200.0</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">items:</span><span style="color:#A6ACCD;"> [</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product B</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product D</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        ]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">user_id:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">total:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">80.0</span><span style="color:#C3E88D;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">items:</span><span style="color:#A6ACCD;"> [</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">},</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">{</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">name:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Product C</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">price:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10.0</span><span style="color:#C3E88D;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">quantity:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        ]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">])</span></span></code></pre></div><h3 id="编写map函数" tabindex="-1">编写map函数 <a class="header-anchor" href="#编写map函数" aria-label="Permalink to &quot;编写map函数&quot;">​</a></h3><blockquote><p>map函数用于将原始数据集合映射为一系列键值对，其中键表示聚合操作的依据，值则是需要聚合的数据。下面是一个简单的例子，该例子使用map函数将订单数据按照用户ID进行分组：</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">var mapFunction </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">function</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> {</span></span>
<span class="line"><span style="color:#A6ACCD;">    emit(this.user_id, { order_total: this.total });</span></span>
<span class="line"><span style="color:#A6ACCD;">};</span></span></code></pre></div><blockquote><p>这里的emit函数用于生成键值对，this表示当前正在处理的文档，this.user_id表示文档中的用户ID，this.total表示文档中的订单总额。</p></blockquote><h3 id="编写reduce函数" tabindex="-1">编写reduce函数 <a class="header-anchor" href="#编写reduce函数" aria-label="Permalink to &quot;编写reduce函数&quot;">​</a></h3><blockquote><p>reduce函数用于对map函数生成的键值对进行聚合操作，将相同键的值聚合为单个结果。下面是一个简单的例子，该例子使用reduce函数将同一用户的订单总额求和：</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">var reduceFunction </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">function</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">key</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">) {</span></span>
<span class="line"><span style="color:#A6ACCD;">    var sum </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">.forEach(</span><span style="color:#F78C6C;">function</span><span style="color:#A6ACCD;">(doc) {</span></span>
<span class="line"><span style="color:#A6ACCD;">        sum </span><span style="color:#89DDFF;">+=</span><span style="color:#A6ACCD;"> doc.order_total;</span></span>
<span class="line"><span style="color:#A6ACCD;">    });</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">return</span><span style="color:#A6ACCD;"> { order_total: sum };</span></span>
<span class="line"><span style="color:#A6ACCD;">};</span></span></code></pre></div><h3 id="执行mapreduce操作" tabindex="-1">执行MapReduce操作 <a class="header-anchor" href="#执行mapreduce操作" aria-label="Permalink to &quot;执行MapReduce操作&quot;">​</a></h3><blockquote><p>有了map和reduce函数后，我们可以使用MongoDB的mapReduce()函数执行MapReduce操作。下面是一个简单的例子，该例子使用上面定义的map和reduce函数，将订单数据按照用户ID进行分组，然后计算每个用户的订单总额：</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.orders.mapReduce(</span></span>
<span class="line"><span style="color:#A6ACCD;">    mapFunction,</span></span>
<span class="line"><span style="color:#A6ACCD;">    reduceFunction,</span></span>
<span class="line"><span style="color:#A6ACCD;">    { </span><span style="color:#F78C6C;">out</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">order_totals</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> }</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><blockquote><p>这里的mapFunction和reduceFunction分别是之前定义的map和reduce函数，{ out: &quot;order_totals&quot; }表示将聚合结果保存到名为order_totals的集合中。</p></blockquote><h3 id="查询结果" tabindex="-1">查询结果 <a class="header-anchor" href="#查询结果" aria-label="Permalink to &quot;查询结果&quot;">​</a></h3><blockquote><p>执行完上述代码后，MongoDB会自动执行MapReduce操作，将结果存储在order_totals集合中。我们可以使用find()函数查询该集合，查看MapReduce操作的结果：</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.order_totals.find</span><span style="color:#89DDFF;">()</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304221715349.png" alt="image-20230422171502270" style="zoom:80%;"><blockquote><p>这样就可以使用MongoDB的MapReduce功能对数据进行聚合处理和分析了。需要注意的是，由于MapReduce操作需要对数据进行分片处理，因此对于大型数据集，可能需要使用sharding等技术来优化性能。</p></blockquote><h1 id="原子操作" tabindex="-1">原子操作 <a class="header-anchor" href="#原子操作" aria-label="Permalink to &quot;原子操作&quot;">​</a></h1><blockquote><p>mongodb不支持事务，所以，在你的项目中应用时，要注意这点。无论什么设计，都不要要求mongodb保证数据的完整性。但是mongodb提供了许多原子操作，比如文档的保存，修改，删除等，都是原子操作。所谓原子操作就是要么这个文档保存到Mongodb，要么没有保存到Mongodb，不会出现查询到的文档没有保存完整的情况。</p></blockquote><h2 id="数据准备-1" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备-1" aria-label="Permalink to &quot;数据准备&quot;">​</a></h2><p>考虑下面的例子，图书馆的书籍及结账信息。</p><p>实例说明了在一个相同的文档中如何确保嵌入字段关联原子操作（update：更新）的字段是同步的。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.insertOne({</span></span>
<span class="line"><span style="color:#A6ACCD;">  _id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  title: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MongoDB: The Definitive Guide</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  author: [ &quot;Kristina Chodorow&quot;, &quot;Mike Dirolf&quot; ],</span></span>
<span class="line"><span style="color:#A6ACCD;">  published_date: ISODate(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">2010-09-24</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  pages: </span><span style="color:#F78C6C;">216</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">language</span><span style="color:#A6ACCD;">: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">English</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  publisher_id: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">oreilly</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  available: </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  checkout: [ { by: &quot;joe&quot;, date: ISODate(&quot;2012-10-15&quot;) } ]</span></span>
<span class="line"><span style="color:#A6ACCD;">})</span></span></code></pre></div><h2 id="常用命令" tabindex="-1">常用命令 <a class="header-anchor" href="#常用命令" aria-label="Permalink to &quot;常用命令&quot;">​</a></h2><h3 id="set" tabindex="-1">$set <a class="header-anchor" href="#set" aria-label="Permalink to &quot;$set&quot;">​</a></h3><p>用来指定一个键并更新键值，若键不存在并创建。我们可以使用 $set 操作符修改 name 字段的值：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$</span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;">: {published_date: new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="unset" tabindex="-1">$unset <a class="header-anchor" href="#unset" aria-label="Permalink to &quot;$unset&quot;">​</a></h3><p>原子地删除一个字段</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$unset: {published_date: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="inc" tabindex="-1">$inc <a class="header-anchor" href="#inc" aria-label="Permalink to &quot;$inc&quot;">​</a></h3><p>$inc可以对文档的某个值为数字型（只能为满足要求的数字）的键进行增减的操作。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$inc: {available: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">}})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$inc: {available: </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">}})</span></span>
<span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$inc: {available: </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="push" tabindex="-1">$push <a class="header-anchor" href="#push" aria-label="Permalink to &quot;$push&quot;">​</a></h3><p>把value追加到field里面去，field一定要是数组类型才行，<strong>如果field不存在，会新增一个数组类型加进去</strong>。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$push: {published_date: new </span><span style="color:#C792EA;">Date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="pull" tabindex="-1">$pull <a class="header-anchor" href="#pull" aria-label="Permalink to &quot;$pull&quot;">​</a></h3><p>从数组field内删除一个等于value值。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$pull: {author: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Mike Dirolf</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="addtoset" tabindex="-1">$addToSet <a class="header-anchor" href="#addtoset" aria-label="Permalink to &quot;$addToSet&quot;">​</a></h3><p>增加一个值到数组内，而且只有当这个值不在数组内才增加。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$addToSet: {author: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">renshuo</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="pop" tabindex="-1">$pop <a class="header-anchor" href="#pop" aria-label="Permalink to &quot;$pop&quot;">​</a></h3><p>删除数组的第一个或最后一个元素</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$pop: {author: </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div><h3 id="rename" tabindex="-1">$rename <a class="header-anchor" href="#rename" aria-label="Permalink to &quot;$rename&quot;">​</a></h3><p>修改字段名称，旧字段名:新字段名</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">db.book.</span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;">({_id: </span><span style="color:#F78C6C;">123456789</span><span style="color:#A6ACCD;">}, {$rename: {author: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">author1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">}})</span></span></code></pre></div></div></div></main><footer class="VPDocFooter" data-v-6b87e69f data-v-37656e44><!--[--><!--]--><!----><nav class="prev-next" data-v-37656e44><div class="pager" data-v-37656e44><a class="pager-link prev" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-37656e44><span class="desc" data-v-37656e44>Previous page</span><span class="title" data-v-37656e44>本地缓存</span></a></div><div class="pager" data-v-37656e44><a class="pager-link next" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-37656e44><span class="desc" data-v-37656e44>Next page</span><span class="title" data-v-37656e44>MongoDB进阶</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"2、数据库_mysql_mysql面试_基础.md\":\"40da680a\",\"1、学前端_5、小程序_小程序项目.md\":\"60a1629b\",\"1、学前端_4、node_知识篇.md\":\"a7fb500e\",\"1、学前端_2、js_ts_es6 进阶.md\":\"6d07ba10\",\"1、学前端_3、vue_vue3_vue3进阶.md\":\"7ac622b4\",\"5、运维_jenkins.md\":\"929081f8\",\"1、学前端_2、js_ts_typescript.md\":\"875a4aa4\",\"2、数据库_mysql_mysql核心_设计.md\":\"7faf46d1\",\"2、数据库_mysql_mysql核心_基础.md\":\"d8e97f3e\",\"1、学前端_1、html_css_html基础.md\":\"7584d076\",\"1、学前端_5、专题篇_问题篇.md\":\"e893aaa2\",\"2、数据库_mysql_mysql面试_进阶.md\":\"f934806d\",\"3、springboot_运维_原理.md\":\"f4a39db6\",\"2、数据库_influxdb.md\":\"6e1711e1\",\"3、springboot_新特性.md\":\"cdf3e307\",\"mybatis_mybatisplus_jpa.md\":\"8e41681b\",\"1、学前端_5、小程序_小程序优化.md\":\"a2185198\",\"2、数据库_redis_redis基础.md\":\"856df0e0\",\"linux_实用脚本.md\":\"f2299dd5\",\"4、微服务_必备_分布式基础.md\":\"d49863d5\",\"2、数据库_redis_redis优化.md\":\"e66ae32f\",\"4、微服务_springsecurity_进阶篇.md\":\"235a8e9e\",\"5、运维_chatgpt.md\":\"10db3823\",\"2、数据库_mysql_分库分表.md\":\"e1c8a095\",\"start.md\":\"9bc1ff8d\",\"5、运维_github.md\":\"2ec6c735\",\"java学前端_css.md\":\"f11b47f0\",\"1、学前端_5、专题篇_知识篇.md\":\"a463ed8d\",\"linux_软件部署.md\":\"d6722925\",\"2、数据库_neo4j.md\":\"97ad22ac\",\"team.md\":\"ce467a6a\",\"nginx_实战篇.md\":\"7785486e\",\"index.md\":\"8c3ec167\",\"计算机基础_计算机网络_网络基础.md\":\"7a54a85d\",\"1、学前端_4、node_进阶篇.md\":\"60f6db69\",\"java_java集合.md\":\"a049b313\",\"1、学前端_3、vue_vue3_vue3高级.md\":\"614d1516\",\"1、学前端_5、小程序_微信小程序.md\":\"9a4be771\",\"5、运维_netty.md\":\"12ca0278\",\"2、数据库_mysql_mysql核心_运维.md\":\"83f97c16\",\"idea_vs code.md\":\"afdcb593\",\"java学前端_vue3_组件.md\":\"1086884e\",\"idea_chrome.md\":\"4a32afbc\",\"云原生_k8s.md\":\"db58e65a\",\"2、数据库_mysql_mysql核心_进阶.md\":\"61d16dff\",\"ssm_springbatch.md\":\"f799ab4a\",\"三高_分布式.md\":\"db1b8a1b\",\"2、数据库_elasticsearch_1、es基础.md\":\"04d17448\",\"linux_linux基础.md\":\"4b0bf394\",\"idea_idea插件.md\":\"fa86e45a\",\"可视化 _ 监控_可视化大屏.md\":\"004553bd\",\"2、数据库_mongodb_整合.md\":\"3c47d7f4\",\"4、微服务_springsecurity_基础篇.md\":\"534a3401\",\"4、微服务_进阶.md\":\"69095c58\",\"计算机基础_计算机基础_操作系统.md\":\"0f75d113\",\"可视化 _ 监控_zabbix.md\":\"71f2270e\",\"nginx_基础篇.md\":\"c7d8bb50\",\"1、学前端_4、node_项目实战.md\":\"bc5065b8\",\"2、数据库_redis_redis原理.md\":\"5cedf685\",\"可视化 _ 监控_监控基础.md\":\"ac56ce4d\",\"三高_高并发.md\":\"ea9ffc99\",\"2、数据库_redis_redis高级.md\":\"1d5872f6\",\"1、学前端_4、node_基础篇.md\":\"581cc13a\",\"2、数据库_mongodb_基础.md\":\"fb7a0a29\",\"idea_idea基础.md\":\"6f2f9638\",\"4、微服务_必备_sentinel.md\":\"2edfbf6c\",\"2、数据库_elasticsearch_3、es高级.md\":\"ef146606\",\"1、学前端_3、vue_vue3_vue3新语法.md\":\"8afd5409\",\"消息中间件_canal.md\":\"3949163c\",\"ssm_maven.md\":\"2c5e12ed\",\"4、微服务_springsecurity_高级篇.md\":\"882d3ff3\",\"linux_linux进阶.md\":\"188ef7b4\",\"计算机基础_设计模式_uml.md\":\"634ba256\",\"计算机基础_算法_leetcode.md\":\"77162fb9\",\"项目实战_小兔鲜_进阶篇1.md\":\"17c52c81\",\"1、学前端_2、js_ts_es6 基础.md\":\"fda3f18b\",\"项目实战_小兔鲜_进阶篇2.md\":\"a0f23006\",\"软件测试_测试基础.md\":\"8c1060cd\",\"2、数据库_redis_本地缓存.md\":\"00617fe6\",\"nginx_面试篇.md\":\"e3fb373a\",\"mybatis_mybatisplus_mybatis.md\":\"9239e0ad\",\"linux_shell.md\":\"ae53d83b\",\"2、数据库_mysql_mysql核心_优化.md\":\"36230425\",\"项目实战_项目推荐.md\":\"f9d97630\",\"mybatis_mybatisplus_mybatisplus.md\":\"0030fd35\",\"项目实战_百度地图_进阶篇.md\":\"c8b93267\",\"三高_高可用.md\":\"323840c5\",\"java_java新特性.md\":\"22abf56d\",\"软件测试_压力测试.md\":\"9ab44440\",\"java学前端_html_js.md\":\"e0fcd240\",\"2、数据库_redis_redis实战.md\":\"d6daeeab\",\"nginx_进阶篇.md\":\"e6b63195\",\"三高_秒杀.md\":\"3878bb64\",\"5、运维_git.md\":\"0264925c\",\"java_java进阶.md\":\"e79cb5b4\",\"并发 _ 多线程_基础篇.md\":\"7adbfac5\",\"项目实战_百度地图_基础篇.md\":\"8afa5954\",\"java学前端_react.md\":\"3ec827dd\",\"1、学前端_1、html_css_css基础.md\":\"01b56712\",\"项目实战_小兔鲜_基础篇.md\":\"646f5df5\",\"1、学前端_2、js_ts_js 基础.md\":\"cb13e36f\",\"可视化 _ 监控_监控进阶.md\":\"0cdbc292\",\"计算机基础_设计模式_基础篇.md\":\"51617287\",\"计算机基础_数据结构_基础篇.md\":\"b2bfd8d4\",\"项目实战_苍穹外卖_进阶篇.md\":\"48415e41\",\"ssm_spring.md\":\"ab514659\",\"消息中间件_rabbitmq.md\":\"45b1eb28\",\"1、学前端_1、html_css_网页进阶.md\":\"db998248\",\"消息中间件_kafka.md\":\"b747dabf\",\"云原生_docker.md\":\"983c7ba7\",\"4、微服务_必备_分布式锁.md\":\"5af1cf8d\",\"消息中间件_rocketmq.md\":\"d441da85\",\"项目实战_黑马头条_基础篇.md\":\"b05af3a6\",\"ssm_springmvc.md\":\"81b9714f\",\"项目实战_支付.md\":\"1d7407dd\",\"项目实战_黑马头条_进阶篇2.md\":\"bff0015b\",\"项目实战_黑马头条_进阶篇.md\":\"19f18388\",\"java学前端_vue2_组件.md\":\"58c6b1df\",\"3、springboot_基础篇.md\":\"529c66f4\",\"3、springboot_应用篇.md\":\"8b92aa61\",\"项目实战_黑马头条_高级篇.md\":\"227c08c1\",\"1、学前端_5、小程序_uniapp.md\":\"71a282b4\",\"项目实战_云尚办公_基础篇.md\":\"1fe188ba\",\"并发 _ 多线程_并发完善.md\":\"26619c46\",\"1、学前端_2、js_ts_js 进阶.md\":\"657dfb8f\",\"java_java高级.md\":\"23782d1a\",\"java_java基础.md\":\"86d67c77\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"VitePress\",\"description\":\"A VitePress site\",\"base\":\"/notebook/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"algolia\":{\"appId\":\"DW7O63I9IR\",\"apiKey\":\"f8ed758cdb288a8b06542bc35923c1a1\",\"indexName\":\"notebook\"},\"sidebar\":[{\"text\":\"Java\",\"collapsed\":true,\"items\":[{\"text\":\"Java基础\",\"link\":\"/Java/Java基础\"},{\"text\":\"Java新特性\",\"link\":\"/Java/Java新特性\"},{\"text\":\"Java进阶\",\"link\":\"/Java/Java进阶\"},{\"text\":\"Java集合\",\"link\":\"/Java/Java集合\"},{\"text\":\"Java高级\",\"link\":\"/Java/Java高级\"}]},{\"text\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux基础\",\"link\":\"/Linux/Linux基础\"},{\"text\":\"Linux新特性\",\"link\":\"/Linux/Linux进阶\"},{\"text\":\"Shell脚本\",\"link\":\"/Linux/Shell\"},{\"text\":\"实用脚本\",\"link\":\"/Linux/实用脚本\"},{\"text\":\"软件部署\",\"link\":\"/Linux/软件部署\"}]},{\"text\":\"Nginx\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/Nginx/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/Nginx/进阶篇\"},{\"text\":\"实战篇\",\"link\":\"/Nginx/实战篇\"},{\"text\":\"面试篇\",\"link\":\"/Nginx/面试篇\"}]},{\"text\":\"SSM\",\"collapsed\":true,\"items\":[{\"text\":\"Maven\",\"link\":\"/SSM/Maven\"},{\"text\":\"Spring\",\"link\":\"/SSM/Spring\"},{\"text\":\"SpringMVC\",\"link\":\"/SSM/SpringMVC\"},{\"text\":\"SpringBatch\",\"link\":\"/SSM/SpringBatch\"}]},{\"text\":\"SpringBoot\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/3、SpringBoot/基础篇\"},{\"text\":\"应用篇\",\"link\":\"/3、SpringBoot/应用篇\"},{\"text\":\"新特性\",\"link\":\"/3、SpringBoot/新特性\"},{\"text\":\"运维&原理\",\"link\":\"/3、SpringBoot/运维&原理\"}]},{\"text\":\"SpringCloud\",\"collapsed\":true,\"items\":[{\"text\":\"SpringCloud\",\"link\":\"/4、微服务/进阶\"},{\"text\":\"Sentinel\",\"link\":\"/4、微服务/必备/Sentinel\"}]},{\"text\":\"SpringSecurity\",\"collapsed\":true,\"items\":[{\"text\":\"SpringSecurity基础篇\",\"link\":\"/4、微服务/SpringSecurity/基础篇\"},{\"text\":\"SpringSecurity进阶篇\",\"link\":\"/4、微服务/SpringSecurity/进阶篇\"},{\"text\":\"SpringSecurity高级篇\",\"link\":\"/4、微服务/SpringSecurity/高级篇\"}]},{\"text\":\"Mybatis & MybatisPlus\",\"collapsed\":true,\"items\":[{\"text\":\"Mybatis\",\"link\":\"/Mybatis&MybatisPlus/Mybatis\"},{\"text\":\"MybatisPlus\",\"link\":\"/Mybatis&MybatisPlus/MybatisPlus\"},{\"text\":\"JPA\",\"link\":\"/Mybatis&MybatisPlus/JPA\"}]},{\"text\":\"Git & ChatGPT\",\"collapsed\":true,\"items\":[{\"text\":\"Git\",\"link\":\"/5、运维/Git\"},{\"text\":\"Github\",\"link\":\"/5、运维/Github\"},{\"text\":\"ChatGPT\",\"link\":\"/5、运维/ChatGPT\"},{\"text\":\"Jenkins\",\"link\":\"/5、运维/Jenkins\"},{\"text\":\"Netty\",\"link\":\"/5、运维/Netty\"}]},{\"text\":\"数据库\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL基础\",\"link\":\"/2、数据库/MySQL/MySQL核心/基础\"},{\"text\":\"MySQL进阶\",\"link\":\"/2、数据库/MySQL/MySQL核心/进阶\"},{\"text\":\"MySQL优化\",\"link\":\"/2、数据库/MySQL/MySQL核心/优化\"},{\"text\":\"MySQL设计\",\"link\":\"/2、数据库/MySQL/MySQL核心/设计\"},{\"text\":\"MySQL运维\",\"link\":\"/2、数据库/MySQL/MySQL核心/运维\"},{\"text\":\"分库分表\",\"link\":\"/2、数据库/MySQL/分库分表\"}]},{\"text\":\"Redis\",\"collapsed\":true,\"items\":[{\"text\":\"Redis基础\",\"link\":\"/2、数据库/Redis/Redis基础\"},{\"text\":\"Redis优化\",\"link\":\"/2、数据库/Redis/Redis优化\"},{\"text\":\"Redis原理\",\"link\":\"/2、数据库/Redis/Redis原理\"},{\"text\":\"Redis高级\",\"link\":\"/2、数据库/Redis/Redis高级\"},{\"text\":\"Redis实战\",\"link\":\"/2、数据库/Redis/Redis实战\"},{\"text\":\"本地缓存\",\"link\":\"/2、数据库/Redis/本地缓存\"}]},{\"text\":\"MongoDB\",\"collapsed\":true,\"items\":[{\"text\":\"MongoDB基础\",\"link\":\"/2、数据库/MongoDB/基础\"},{\"text\":\"MongoDB进阶\",\"link\":\"/2、数据库/MongoDB/整合\"}]},{\"text\":\"ElasticSearch\",\"collapsed\":true,\"items\":[{\"text\":\"ES基础\",\"link\":\"/2、数据库/ElasticSearch/1、ES基础\"},{\"text\":\"ES高级\",\"link\":\"/2、数据库/ElasticSearch/3、ES高级\"}]},{\"text\":\"InfluxDB\",\"link\":\"/2、数据库/influxdb\"},{\"text\":\"Neo4j\",\"link\":\"/2、数据库/Neo4j\"}]},{\"text\":\"高并发 & 秒杀 & 分布式\",\"collapsed\":true,\"items\":[{\"text\":\"分布式理论\",\"link\":\"/三高/分布式\"},{\"text\":\"分布式锁\",\"link\":\"/4、微服务/必备/分布式锁\"},{\"text\":\"秒杀\",\"link\":\"/三高/秒杀\"},{\"text\":\"高可用\",\"link\":\"/三高/高可用\"},{\"text\":\"高并发\",\"link\":\"/三高/高并发\"}]},{\"text\":\"云原生\",\"collapsed\":true,\"items\":[{\"text\":\"Docker\",\"link\":\"/云原生/Docker\"},{\"text\":\"K8S\",\"link\":\"/云原生/K8S\"}]},{\"text\":\"可视化 & 监控\",\"collapsed\":true,\"items\":[{\"text\":\"监控基础\",\"link\":\"/可视化 & 监控/监控基础\"},{\"text\":\"监控进阶\",\"link\":\"/可视化 & 监控/监控进阶\"},{\"text\":\"可视化大屏\",\"link\":\"/可视化 & 监控/可视化大屏\"},{\"text\":\"Zabbix\",\"link\":\"/可视化 & 监控/Zabbix\"}]},{\"text\":\"学前端\",\"collapsed\":true,\"items\":[{\"text\":\"HTML+CSS\",\"collapsed\":true,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/1、学前端/1、HTML+CSS/HTML基础\"},{\"text\":\"CSS基础\",\"link\":\"/1、学前端/1、HTML+CSS/CSS基础\"},{\"text\":\"网页进阶\",\"link\":\"/1、学前端/1、HTML+CSS/网页进阶\"}]},{\"text\":\"JS+TS\",\"collapsed\":true,\"items\":[{\"text\":\"JS基础\",\"link\":\"/1、学前端/2、JS+TS/JS 基础\"},{\"text\":\"JS进阶\",\"link\":\"/1、学前端/2、JS+TS/JS 进阶\"},{\"text\":\"ES6基础\",\"link\":\"/1、学前端/2、JS+TS/ES6 基础\"},{\"text\":\"ES6进阶\",\"link\":\"/1、学前端/2、JS+TS/ES6 进阶\"},{\"text\":\"TS基础\",\"link\":\"/1、学前端/2、JS+TS/TypeScript\"}]},{\"text\":\"NodeJS\",\"collapsed\":true,\"items\":[{\"text\":\"Node基础\",\"link\":\"/1、学前端/4、Node/基础篇\"},{\"text\":\"Node进阶\",\"link\":\"/1、学前端/4、Node/进阶篇\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/4、Node/项目实战\"}]},{\"text\":\"Vue\",\"collapsed\":true,\"items\":[{\"text\":\"Vue3进阶\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3进阶\"},{\"text\":\"Vue3高级\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3高级\"},{\"text\":\"Vue3新语法\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3新语法\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/3、Vue/Vue2/Vue2项目\"}]},{\"text\":\"小程序\",\"collapsed\":true,\"items\":[{\"text\":\"小程序基础\",\"link\":\"/1、学前端/5、小程序/微信小程序\"},{\"text\":\"小程序优化\",\"link\":\"/1、学前端/5、小程序/小程序优化\"},{\"text\":\"uniapp\",\"link\":\"/1、学前端/5、小程序/uniapp\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/5、小程序/小程序项目\"}]}]},{\"text\":\"计算机基础\",\"collapsed\":true,\"items\":[{\"text\":\"数据结构\",\"link\":\"/计算机基础/数据结构/基础篇\"},{\"text\":\"操作系统\",\"link\":\"/计算机基础/计算机基础/操作系统\"},{\"text\":\"设计模式\",\"link\":\"/计算机基础/设计模式/基础篇\"},{\"text\":\"计算机网络\",\"link\":\"/计算机基础/计算机网络/网络基础\"},{\"text\":\"UML\",\"link\":\"/计算机基础/设计模式/UML\"},{\"text\":\"LeetCode\",\"link\":\"/计算机基础/算法/LeetCode\"}]},{\"text\":\"项目实战\",\"collapsed\":true,\"items\":[{\"text\":\"云尚办公\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/云尚办公/基础篇\"}]},{\"text\":\"小兔鲜\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/小兔鲜/基础篇\"},{\"text\":\"进阶篇1\",\"link\":\"/项目实战/小兔鲜/进阶篇1\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/小兔鲜/进阶篇2\"}]},{\"text\":\"地图\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/百度地图/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/百度地图/进阶篇\"}]},{\"text\":\"苍穹外卖\",\"collapsed\":true,\"items\":[{\"text\":\"进阶篇\",\"link\":\"/项目实战/苍穹外卖/进阶篇\"}]},{\"text\":\"黑马头条\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/黑马头条/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/黑马头条/进阶篇\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/黑马头条/进阶篇2\"},{\"text\":\"高级篇\",\"link\":\"/项目实战/黑马头条/高级篇\"}]},{\"text\":\"支付\",\"link\":\"/项目实战/支付\"},{\"text\":\"项目推荐\",\"link\":\"/项目实战/项目推荐\"}]},{\"text\":\"团队成员\",\"link\":\"/team\"}],\"siteTitle\":\"任硕的文档\",\"logo\":\"/Vue.png\",\"nav\":[{\"text\":\"Java学前端\",\"items\":[{\"items\":[{\"text\":\"HTML+JS\",\"link\":\"/Java学前端/HTML+JS\"},{\"text\":\"CSS\",\"link\":\"/Java学前端/CSS\"},{\"text\":\"Vue2+组件\",\"link\":\"/Java学前端/Vue2+组件\"},{\"text\":\"Vue3+组件\",\"link\":\"/Java学前端/Vue3+组件\"},{\"text\":\"React\",\"link\":\"/Java学前端/React\"}]}],\"activeMatch\":\"/Java/\"},{\"text\":\"软件测试\",\"items\":[{\"items\":[{\"text\":\"测试基础\",\"link\":\"/软件测试/测试基础\"},{\"text\":\"压力测试\",\"link\":\"/软件测试/压力测试\"}]}]},{\"text\":\"多线程\",\"items\":[{\"items\":[{\"text\":\"基础篇\",\"link\":\"/并发 & 多线程/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/并发 & 多线程/并发完善\"}]}]},{\"text\":\"开发工具\",\"items\":[{\"items\":[{\"text\":\"Chrome\",\"link\":\"/IDEA/Chrome\"},{\"text\":\"IDEA基础\",\"link\":\"/IDEA/IDEA基础\"},{\"text\":\"IDEA插件\",\"link\":\"/IDEA/IDEA插件\"},{\"text\":\"VS Code\",\"link\":\"/IDEA/VS Code\"}]}]},{\"text\":\"消息中间件\",\"items\":[{\"items\":[{\"text\":\"RabbitMQ\",\"link\":\"/消息中间件/RabbitMQ\"},{\"text\":\"RocketMQ\",\"link\":\"/消息中间件/RocketMQ\"},{\"text\":\"Kafka\",\"link\":\"/消息中间件/Kafka\"},{\"text\":\"Canal\",\"link\":\"/消息中间件/Canal\"}]}]}],\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/renshuo123/renshuo123.github.io\"},{\"icon\":\"twitter\",\"link\":\"#\"},{\"icon\":{\"svg\":\"<svg t=\\\"1676028692954\\\" class=\\\"icon\\\" ...</path></svg>\"},\"link\":\"https://github.com/\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>